ACC: How to Use Automation to Run a Microsoft Word Macro
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 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:
Q160294
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 INFORMATIONExample - 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
C:\My Documents folder.
- Click OK to close the Insert Object dialog box.
- Display the property sheet for the object frame control. Set the Name
property to OLEObj.
- Add a command button to the form and set its Name property to RunMacro.
- 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
- 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 : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto
|