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

ID: Q154570

The information in this article applies to:

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:

   ARTICLE-ID: Q160294
   TITLE     : 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.

MORE INFORMATION

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:

1. Open Microsoft Word 7.0. When you run the code from Microsoft Access,

   Microsoft Word remains open.

2. Open a Microsoft Access database and create a new module.

3. 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

4. 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:

1. Open a Microsoft Access database and create a new blank form.

2. Add an unbound object frame to the form. In the Insert Object dialog

   box, click the Create From File option and choose WordTest.Doc from
your
   C:\My Documents folder.

3. Click OK to close the Insert Object dialog box.

4. Display the property sheet for the object frame control. Set the Name

   property to OLEObj.

5. Add a command button to the form and set its Name property to RunMacro.

6. 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

7. 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.

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).

REFERENCES

For more information about using Automation, search the Help Index for "Automation," or ask the Microsoft Access 97 Office Assistant.

Additional query words:

Keywords          : kbinterop IntpOlea 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998