Microsoft KB Archive/274703

= OFF2001: How to Run Sample Code from Knowledge Base Articles =

Article ID: 274703

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Excel 2001 for Mac
 * Microsoft PowerPoint 2001 for Mac
 * Microsoft Word 2001 for Mac

-



This article was previously published under Q274703



For a Microsoft Office XP version of this article, see 290140.

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 98 Macintosh Edition version of this article, see 181058.



SUMMARY
The Microsoft Knowledge Base includes a number of articles that contain sample code for Visual Basic for Applications macros. Many of these macros illustrate basic concepts of programming in Visual Basic for Microsoft Office. This article contains instructions for 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. Microsoft Excel stores macros in workbooks, Microsoft PowerPoint stores macros in presentations, and Microsoft Word stores macros in templates and documents. To use the sample code in a Knowledge Base article, you must add a new Visual Basic macro (or select an existing macro) in an Excel workbook, PowerPoint presentation, or Word template or document. You can then paste or type the sample code into this macro. Use the following methods to create a new macro, insert the sample code, and run the macro.

To Create a New Macro
 Open or create an Excel workbook, PowerPoint presentation, or Word template or document as appropriate. On the Tools menu, point to Macro, and then click Macros. In the Macros In list, click to select the Excel workbook, PowerPoint presentation, or Word template or document where you want to save the macro. In the Macro Name box, type a name for the macro. Click Create. This starts the Visual Basic Editor.  Type the macro code.

NOTE: Type the macro code between the first line Sub and the last line: End Sub To add the sample code to the macro using the Clipboard, follow these steps:

<ol style="list-style-type: lower-alpha;"> Switch to the program that you are using to view the Knowledge Base article, such as Microsoft Internet Explorer.</li> Select only the sample code between the first line, &quot;Sub &quot;, and the last line, &quot;End Sub&quot;.</li> On the Edit menu, click Copy.</li> Switch to the Visual Basic Editor.</li> In the Visual Basic Editor, click to place the insertion point between the first line, &quot;Sub &quot;, and the last line, &quot;End Sub&quot;, of your macro.</li> On the Edit menu, click Paste.</li></ol> </li></ol>

To Add the Sample Code to a Module
To add code to a module, you must have an existing module, or you must create a new module. To create a new module, follow these steps: <ol> Open or create an Excel workbook, PowerPoint presentation, or Word template or document, as appropriate.</li> On the Tools menu, point to Macro, and then click Visual Basic Editor.</li>  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 workbook, presentation, template, or document. If you have not saved your workbook, presentation, template, or document, the project name is based on the program that you are using. <pre class="fixed_text">     Program                Default project name ---

Microsoft Excel       VBAProject(Book1)

Microsoft PowerPoint  VBAProject(Presentation1)

Microsoft Word        Project(Document1) -or- TemplateProject(Template1) NOTE: If you want to store your macro in the default Microsoft Word template, Normal.dot, double-click the Normal project. </li> If the project you clicked does not contain a folder called Modules underneath it, click Module on the Insert menu.</li> 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.</li> Type the sample code exactly as it appears in the Knowledge Base article, including the &quot;Sub &quot; and &quot;End Sub&quot; lines. Use the TAB key to indent text.

NOTE: 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.</li></ol>

To Run the Macro

 * 1) Switch from the Visual Basic Editor to Excel, PowerPoint, or Word, as appropriate.
 * 2) On the Tools menu, point to Macro, and then click Macros.
 * 3) Click the name of the macro in the Macro Name list, and then click Run.

<div class="references_section">