ACC2000: How To Run a Microsoft Word 2000 Macro Using Automation (212890)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q212890
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to use Automation to run a Microsoft Word 2000 macro from Microsoft Access.

MORE INFORMATION

By using Automation code in Microsoft Access, you can open a Microsoft Word 2000 document and run a macro in the document.

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

These examples assume that you have installed Word 2000, that you have created a document called WordTest.doc (on drive C), and that a macro called Macro1 exists in the default template (Normal.dot).

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

  1. Start Microsoft Access and open any database or project.
  2. In the Database window, click Modules, and then click New.
  3. On the Tools menu, click References.
  4. Click Microsoft Word 9.0 Object Library in the Available References box, and then click OK.
  5. Type or paste 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:\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
    					
  6. To test this function, type the following line in the Immediate window, and then press ENTER:

    ?RunWordMacro()

    Note that Microsoft Word opens the Wordtest.doc document on drive C, and then runs the macro called Macro1.

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

  1. Start Microsoft Access and open any database or project.
  2. In the Database window, click Modules, and then click New.
  3. On the Tools menu, click References.
  4. Click Microsoft Word 9.0 Object Library in the Available References box, and then click OK.
  5. On the File menu, click Close and Return to Microsoft Access.
  6. In the Database window, click Forms, and then click New.
  7. Add an unbound object frame control to the Detail section of the form.
  8. In the Insert Object dialog box, click Create from File, and then, in the File box, type the path and location of the Word file with the macro. For this example, type the following path in the File box:

    C:\WordTest.doc

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

    Name: MacroObj
    Locked: No

  10. Add a command button to the form, and then set its Name property to RunWordMacro. Set its On Click 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
    					
  11. On the View menu, click Form View.
  12. Click the command button on the form, and then note that the macro runs while the document is edited in place in the control on your form.

REFERENCES

For more information about Automation, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type understanding automation in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

For more information about Verb and Action properties, click Microsoft Access Help on the Help menu, type verb property in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about in-place activation, click Microsoft Access Help on the Help menu, type autoactivate property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbinfo kbinterop KB212890