Microsoft KB Archive/154570

= ACC: How to Use Automation to Run a Microsoft Word Macro =

Article ID: 154570

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition
 * Microsoft Word 95 Standard Edition

-



This article was previously published under Q154570



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to use Automation to run a Microsoft Word 7.0 macro. For more information about how to use Automation to run a Microsoft Word 97 macro, please see the following article in the Microsoft Knowledge Base:

160294 ACC: How to Use Automation to Run a Microsoft Word 97 Macro

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



Example - Run a Microsoft Word 7.0 Macro
The following example opens a Microsoft Word document called Wordtest.doc and runs a macro, Macro1, which currently exists in the default template. The following code example assumes that you have created a document named WordTest.Doc in the C:\My Documents folder and that you have created a macro named Macro1 in the default template.

To run this code:

 Open Microsoft Word 7.0. When you run the code from Microsoft Access, Microsoft Word remains open. Open a Microsoft Access database and create a new module.  Type the following procedure: Function RunMacro Dim WordObj as Object Set WordObj = CreateObject("Word.Basic") WordObj.FileOpen "C:\My Documents\Wordtest.doc" WordObj.ToolsMacro "Macro1", 1 End Function  To run the function, open the Debug Window, type the following line, and then press ENTER:

? RunMacro



Example - Run a Microsoft Word 7.0 Macro in a Form's Object Frame
This procedure activates an embedded Microsoft Word object on a form and runs a macro named Macro1, which currently exists in the Microsoft Word default template. The following code assumes that you have created a document named WordTest.Doc in the C:\My Documents folder and that you have created a macro named Macro1 in the Microsoft Word default template.

To run this code:

 Open a Microsoft Access database and create a new blank form.  Add an unbound object frame to the form. In the Insert Object dialog box, click the Create From File option and choose WordTest.Doc fromyour <pre class="fixed_text">  C:\My Documents folder. </li> Click OK to close the Insert Object dialog box.</li> Display the property sheet for the object frame control. Set the Name property to OLEObj.</li> Add a command button to the form and set its Name property to RunMacro.</li>  Type the following function for the OnClick event procedure of the command button: Private Sub RunMacro_Click Dim WordObj As Object Me![OLEObj].Verb = acOLEVerbOpen   ' Tells Microsoft Access to                                             ' open the application. Me![OLEObj].Action = acOLEActivate ' Activates the application. Set WordObj = Me![OLEObj].Object.Application.WordBasic WordObj.ToolsMacro "Macro1", 1 WordObj.FileClose 2 End Sub </li> Open the form in Form view and click the command button. Microsoft Word becomes the active application, runs the macro, and then returns to the form.</li></ol>

NOTE: In both examples, when the Automation object variable goes out of scope, the instance of Microsoft Word is unloaded, unless the object was created from a previous instance (already open).

<div class="references_section">