Microsoft KB Archive/172483

= HOWTO: Run a Word 97 Macro That Requires Arguments =

Article ID: 172483

Article Last Modified on 1/20/2007

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q172483



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 with Word 97 cannot be used with Automation to run a 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.

NOTE: The Run method has been updated in Word 2000 so that you can use that method to run a macro that requires arguments.



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 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
 Start a new Word document (Document1). Press ALT+F11 to start the Visual Basic Editor. Click Project Explorer on the View menu to display the Project Explorer. In the Project Explorer, locate the "ThisDocument" object for Document1's project. Right-click "ThisDocument" and select View Code.  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  Press ALT+Q to exit the Visual Basic Editor and return to Document1. Save this document as C:\TEST.DOC, and exit Word.</li></ol>

Steps to Create the Visual Basic Application
<ol> Start a new "Standard EXE" project.</li> On the Project menu, click References. Check "Microsoft Word 8.0 Object Library," and then click OK.</li>  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 </li> Press the F5 key to run the application. Click Form1 to run the Word macro.</li></ol>

Keywords: kbautomation kbhowto kbinterop KB172483

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.