ACC: Running a Microsoft Word Macro Using Automation

Last reviewed: August 28, 1997
Article ID: Q154570
The information in this article applies to:
  • Microsoft Access versions 7.0, 97
  • Microsoft Word for Windows 95 version 7.0

SUMMARY

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

This article demonstrates how to use Automation to run a Microsoft Word 7.0 macro.

For more information about running a Microsoft Word 97 macro through Automation, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q160294
   TITLE     : ACC: Running a Microsoft Word 97 Macro Using Automation

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

Through Automation, it is possible to run a Microsoft Word macro. This example uses the WordBasic ToolsMacro statement in Microsoft Word.

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 examples 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 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. In Design view, create an unbound object frame on 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.

Keywords          : AutoGnrl kbinterop PgmHowTo IntpOleA
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


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


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: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.