Microsoft KB Archive/171988

= How To Pass Parameters to a Word 97 Macro =

Article ID: 171988

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q171988



SUMMARY
When using OLE automation to Microsoft Word 97, it is often desirable to pass parameters from Visual FoxPro to the Word 97 object. The Run method is normally used to execute a Word macro through OLE automation; however, the Run method cannot accept parameters. To pass parameters to the Word object, there must be a public Word macro that is a method of a document or template.



MORE INFORMATION
In Word 97, documents and templates have their own Visual Basic for Applications projects: a document project and a template project. Macros can be added as a Public Sub to either the document or template project. For illustration purposes, this article will create a public macro using Word default Normal.dot. If the public macro is added to a document, that document must be opened in order to call the macro.

If the public macro MainMacro is added to a user template, named Testdot.dot stored in the Word directory on the C drive, for example, the following code can be substituted for the oWordDoc=oWordObj.Documents.Add in the Visual FoxPro code in the section below: oWordDoc=oWordObj.documents.Add("C:\word\testdot.dot")

Steps to create the Public Word macro
 Start Microsoft Word 97. Switch to the Visual Basic Editor by pressing ALT+F11 keys or point to Macro on the Tools menu, and click Visual Basic Editor. On the View menu, click Project Explorer. In the Project Explorer, open the Normal template project. Open the Microsoft Word Objects\ThisDocument module within the template project. Select the ThisDocument module. On the View menu, click Code or right-click the ThisDocument module and select View Code.</li>  In the code window for Normal-ThisDocument, enter the following macro: Public Sub Mainmacro(str As String, int1 As Integer) MsgBox str        'Display the string from VFP MsgBox int1       'Display the number from VFP End Sub </li> On the File menu, click Save Normal, then re-select the File menu and click "Close and Return to Microsoft Word."</li></ol>

Visual FoxPro code
Create a new program and enter the following code: CLEAR PUBLIC cname,nnum cname=SPACE(15) nnum=0 @ 2,2 SAY "Enter a name to display" GET cname @ 4,2 SAY "Now enter a number    " GET nnum READ oWordObj=CREATEOBJECT('word.application') oWordDoc=oWordObj.Documents.Add oWordDoc.MainMacro(cname,nnum) oWordObj.QUIT

<div class="references_section">