OFF2000: How to Run Sample Code from Knowledge Base Articles

ID: Q212536


The information in this article applies to:
  • Microsoft Office 2000
  • Microsoft Word 2000
  • Microsoft PowerPoint 2000 For Windows
  • Microsoft Excel 2000
  • Microsoft FrontPage 2000


SUMMARY

The Microsoft Knowledge Base contains a number of articles that include sample code for Visual Basic for Applications macros. Many of these macros illustrate basic concepts of programming in Visual Basic for the various Office 2000 applications. This article contains instructions on how to use the sample code to create a new macro, and how to run that macro.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
To use the sample code from a Knowledge Base article, you must add a new Visual Basic macro (or select an existing macro). You can then copy/paste or type the sample code into this macro by using any of the following methods:

To Create a New Macro

  1. Open or Create a template in the Office application you are using.


  2. On the Tools menu, point to Macro, and then click Macros.


  3. In the Macros In list, select the Word template or document where you want to save the macro.


  4. In the Macro Name box, type a name for the macro.


  5. Click Create to open the Visual Basic Editor.


  6. Do one of the following:



  • Type the macro code between the Sub Macroname() line and the End Sub line.

Sub macroname()
    Type your macro code here
End Sub 

-or-

  • Copy and paste the sample code to your macro by following these steps:

Sub macroname()
 
End Sub 

  1. Switch to the program that you are using to view the Microsoft Knowledge Base article, such as Microsoft Internet Explorer.


  2. To select the sample code, hold the CTRL key and then single-click the code.


  3. On the Edit menu, click Copy.


  4. Switch to the Visual Basic Editor.


  5. In the Visual Basic Editor, select the existing code text of Sub Macroname() to the End Sub of the macro.


  6. On the Edit menu, click Paste.

    NOTE: When you paste, the existing code text will be replaced with the code you copied from the Knowledge Base article.


After typing or pasting the code for your macro, on the File menu, click Close and Return to Microsoft <application>

where <application> is the Microsoft Office 2000 application that you are using. NOTE: Use the TAB key to indent text. Lines that begin with an apostrophe (') are comments and are not required to run the macro. However, you may want to type them in to make it easier to understand the macro.

To Add the Sample Code to a Module

To add code to a module, you must either have an existing module or create a new module. To do this, follow these steps:
  1. Open or Create a template in the Office application you are using.


  2. On the Tools menu, point to Macro and then click Visual Basic Editor.


  3. In the project window of the Visual Basic Editor (by default this is in the upper left corner), click the project icon with the same name as your template or document. If you haven't yet saved your template or document, the project name will be similar to either Project(Document1) or TemplateProject(Template1).

    NOTE FOR WORD USERS: If you want to store your macro in the default global template, Normal.dot, double-click the Normal project.


  4. If the project you clicked does not contain a folder called Modules underneath it, click Module on the Insert menu.


  5. Click anywhere in the code window of the module you want to create. The code window is usually in the upper right corner of the Visual Basic Editor.


  6. Type or paste the sample code exactly as it appears in the Knowledge Base article, including the Sub Macroname() and End Sub lines.

    NOTE: For more information about typing or pasting the macro code, please see the "To Create a New Macro" section in this article.


After typing or pasting the code for your macro, on the File menu, click Close and Return to Microsoft <application>

where <application> is the Microsoft Office 2000 application that you are using.

To Run the Macro

  1. On the Tools menu, point to Macro and then click Macros.


  2. Select the name of the macro in the Macro Name list and then click Run.



REFERENCES

Microsoft Help for each Office program contains numerous topics about working with macros. Visual Basic Help contains extensive information about writing your own code.

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

Q226118 OFF2000: Programming Resources for Visual Basic for Applications

Additional query words: vba vbe visual basic OFF2000

Keywords : kbmacro kbprg kbdta kbwordvba wd2000
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto kbinfo


Last Reviewed: July 26, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.