Microsoft KB Archive/290140

= How to run the sample code for the Office XP programs from Knowledge Base articles =

Article ID: 290140

Article Last Modified on 5/7/2007

-

APPLIES TO


 * Microsoft Excel 2002 Standard Edition
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q290140



For a Microsoft Office 2000 version of this article, see 212536.

For a Microsoft Office 97 version of this article, see 173707.

For a Microsoft Office 2001 for Mac version of this article, see 274703.

For a Microsoft Office 98 Macintosh Edition version of this article, see 181058.



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 XP programs. 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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-and-paste or type the sample code into this macro by using any of the following methods.

To Create a New Macro
 Open or create a template in the Office program that you are using. On the Tools menu, point to Macro, and then click Macros. In the Macros in list, select the Word template or document where you want to save the macro. In the Macro Name box, type a name for the macro. Click Create to open the Visual Basic Editor. Do one of the following: <ul>  Type the macro code between the Sub Macroname line and the End Sub line. Sub macroname Type your macro code here End Sub -or- </li>  Copy and paste the sample code to your macro by following these steps: Sub macroname End Sub <ol style="list-style-type: lower-alpha;"> Switch to the program that you are using to view the Microsoft Knowledge Base article, such as Microsoft Internet Explorer.</li> To select the sample code, hold the CTRL key and then single-click the code.</li> On the Edit menu, click Copy.</li> Switch to the Visual Basic Editor.</li> In the Visual Basic Editor, select the existing code text of Sub Macroname to the End Sub of the macro.</li> On the Edit menu, click Paste.

NOTE: When you paste, the existing code text is replaced with the code that you copied from the Knowledge Base article.</li></ol> </li></ul> </li> After typing or pasting the code for your macro, click Close and Return to Microsoft  on the File menu where   is the Microsoft Office XP program that you are using.</li></ol>

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 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 program that 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 have not 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.
 * 1) If the project that you clicked does not contain a folder called Modules underneath it, click Module on the Insert menu.
 * 2) Click anywhere in the code window of the module that you want to create. The code window is usually in the upper-right corner of the Visual Basic Editor.
 * 3) 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 &quot;To Create a New Macro&quot; section in this article.

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

where  is the Microsoft Office XP program 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.

<div class="references_section">