HOWTO: Run a Word 97 Macro that Requires Arguments

Last reviewed: September 29, 1997
Article ID: Q172483
The information in this article applies to:
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions for Windows, version 4.0
  • Microsoft Word 97 for Windows

SUMMARY

When using Microsoft Word 97 as an ActiveX server, you can run a Word VBA macro by using Word's Run method. However, the Run method cannot be used with automation to run a Word macro that requires arguments. To run a Word macro that requires arguments, you can make your Word macro a method of your document or template. This article illustrates how to do this.

MORE INFORMATION

To make a macro act as a method for your document or template, add the macro as a Public Sub to the ThisDocument object in the Visual Basic Editor for Microsoft Word.

The following steps demonstrate how to create a Visual Basic application that executes a custom method of a Word document. This method expects two arguments, a string and an integer.

Steps to Create the Word Macro

  1. Start a new Word document (Document1).

  2. Press Alt+F11 to start the Visual Basic Editor.

  3. Click Project Explorer on the View menu to display the Project Explorer.

  4. In the Project Explorer, locate the "ThisDocument" object for Document1's project. Right-click "ThisDocument" and select View Code.

  5. In the code window for ThisDocument, add the following macro:

          Public Sub Macro1(s As String, i As Integer)
    
             MsgBox s        'Display the string
             MsgBox i * 10   'Display the product i and 10
          End Sub
    
    

  6. Press Alt+Q to exit the Visual Basic Editor and return to Document1.

  7. Save this document as C:\TEST.DOC and exit Word.

Steps to Create the Visual Basic Application

  1. Start a new "Standard EXE" project.

  2. On the Project menu, click References. Check "Microsoft Word 8.0 Object Library", and click OK.

  3. Add the following code to Form1.

          Option Explicit
    

          Dim objWordApp As Word.Application
          Dim objWordDoc As Word.Document
    

          Private Sub Form_Click()
    

             'Create a new instance of Word
             Set objWordApp = New Word.Application
    
             'Open the document containing the macro
             Set objWordDoc = _
                objWordApp.Documents.Open("C:\TEST.DOC")
    
             'Run the macro
             objWordDoc.macro1 "Hello!", 23
    
             'Close the document and clear the variables
             objWordDoc.Close
             Set objWordDoc = Nothing
             Set objWordApp = Nothing
    
          End Sub
    
    

  4. Press the F5 keys to run the application. Click Form1 to run the Word macro.
Keywords          : VB4ALL VB4WIN vb5all vb5howto vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:4.0 5.0 97


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.