ACC: Running a Microsoft Word 97 Macro Using Automation

Last reviewed: August 29, 1997
Article ID: Q160294
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

This article describes a technique to run a Microsoft Word 97 macro from Microsoft Access using Automation.

For information about how to run a macro in earlier versions of Microsoft Word, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q154570
   TITLE     : ACC: Running a Microsoft Word 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

Using Automation code in Microsoft Access, you can open a Microsoft Word 97 document and execute a macro in the document.

The following examples use two methods, one that opens a Microsoft Word 97 document that is external to Microsoft Access, and one that opens a document that is embedded in a Microsoft Access form.

These examples assume that you have Microsoft Word 97 set up on your computer, that you have a document called C:\My Documents\WordTest.doc, and that a macro called Macro1 exists in the default template (Normal.dot).

Example 1: Run a Macro in an External Microsoft Word 97 Document

  1. Open the sample database Northwind.mdb.

  2. Create a new module in Design view.

  3. On the Tools menu, click References.

  4. Click Microsoft Word 8.0 Object Library in the Available References box. If that selection does not appear, click the Browse button and look for a file called Msword8.olb, which is installed in the C:\Program Files\Microsoft Office\Office folder by default.

  5. Click OK in the References dialog box.

  6. Type the following procedure in the module:

          Function RunWordMacro()
    
             Dim WordApp As Word.Application
             Dim WordDoc As Word.Document
             Set WordApp = CreateObject("Word.Application")
             Set WordDoc = WordApp.Documents.Open _
                 ("C:\My Documents\Wordtest.doc")
             WordApp.Visible = True
             WordApp.Run "Macro1"
    
             ' Uncomment the next line of code to print the document.
             ' WordDoc.PrintOut Background:=False
    
             ' Uncomment the next line of code to save the modified document.
             ' WordDoc.Save
    
             WordApp.Quit SaveChanges:=wdDoNotSaveChanges
             Set WordApp = Nothing
          End Function
    
    

  7. To test this function, type the following line in the Debug window, and then press ENTER.

          ?RunWordMacro()
    

    Note that Microsoft Word 97 opens the C:\My Documents\Wordtest.doc document, and then runs the macro called Macro1.

Example 2: Run a Macro in an Embedded Microsoft Word 97 Document

  1. Open the sample database Northwind.mdb.

  2. Open any module in Design view.

  3. On the Tools menu, click References.

  4. Click Microsoft Word 8.0 Object Library in the Available References box. If that selection does not appear, click the Browse button and look for a file called Msword8.olb, which is installed in the C:\Program Files\Microsoft Office\Office folder by default.

  5. Click OK in the References dialog box.

  6. Create a new form not based on any table or query in Design view.

  7. Add an unbound object frame control to the detail section of the form.

  8. When the Insert Object dialog box appears, click Create From File, and then click the Browse button to select your C:\My Documents\WordTest.doc file.

  9. Click Open in the Browse dialog box, and then click OK in the Insert Object dialog box.

  10. Set the following properties for the unbound object frame control:

           Unbound Object Frame
           --------------------
           Name: MacroObj
           Locked: No
    

  11. Add a command button to the form; set its Name property to RunWordMacro

        and set its OnClick property to the following event procedure:
    

          Private Sub RunWordMacro_Click()
    
             Dim WordObj As Word.Application
    
             ' Open Microsoft Word 97 in place and activate it.
             Me![MacroObj].Verb = -4
             Me![MacroObj].Action = 7
    
             Set WordObj = Me![MacroObj].Object.Application
             WordObj.Run "Macro1"
             Set WordObj = Nothing
          End Sub
    
    

  12. Save the form as frmMacro, and then open it in Form view.

  13. Click the command button on the form and note that the macro runs while

        the document is edited in place in the control on your form.
    

REFERENCES

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

For more information about Verb and Action properties, search the Help Index for "Verb property" or "Action property," or ask the Microsoft Access 97 Office Assistant.

For more information about in-place activation, search the Help Index for "in-place activation," or ask the Microsoft Access 97 Office Assistant.

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


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


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