Using Automation Code

Automation code is the tool you need to use when creating or working with Word documents in Access VBA. Automation code is not a special programming language, just a set of functions used in VBA code to work with the object models of other applications.

All Automation code starts with one of the two functions described as follows, either of which sets a reference to a high-level Automation object. When working with Word, this is generally the Word Application object.

The CreateObject function with "Word.Application" as the Class argument creates a new Word instance; it works whether or not Word is already open. The GetObject function with "Word.Application" as the Class argument attempts to set a reference to an existing instance of Word. GetObject succeeds in setting a reference only if Word is already running.

To avoid creating multiple instances of Word, I use the GetObject function in the body of a procedure, which sets a reference to an existing Word Application object, if Word is running, in combination with an error handler that uses CreateObject to create a new Word Application object if GetObject fails with Error 429, "ActiveX component can't create object":

Set appWord = GetObject(Class:="Word.Application")

[body of procedure here]


Set appWord = Nothing Exit Sub


If Err = 429 Then

'Word is not running; open Word with CreateObject Set appWord = CreateObject(Class:="Word.Application") Resume Next Else

& "; Description: " & Err.Description Resume ErrorHandlerExit End If

In the error handler in the preceding code segment, the appWord Application object variable is set to Nothing. If you want to close down Word when your code has run, this is appropriate; but if you want to preserve the Word Application object for future use (generally a good idea), either comment out this line or delete it, as I have done in most of the procedures in the Word Export sample database.

To work with objects in an object model, first you need to set up a reference to the Application object, or some other object that can be used with the CreateObject or GetObject function. Although you use CreateObject or GetObject to set a reference directly to a Word Document object, generally it is best to create (or set a reference to) a Word Application object, and then use the appWord variable to get at other Word objects below the Application object, because many of the properties and methods you need to use in Automation code belong to the Application object, and you can access all the other objects through the Application object..

The Word Object Model

An object model is a representation of application components that are available for control by Automation code. Typically, most (but not all) of an application's functionality is represented by objects in the object model, letting you do almost anything you can do in the interface, and perhaps a few things that can't be done in the interface. The Word object model is very extensive, but fortunately, in order to work with Word documents and templates, and fill them with data from Access, you need to work with only a few components of the object model — in particular the Application object, Documents collection and Document object, the Tables collection and Table object, and the Bookmarks collection and Bookmark object. These object model components are the ones used in the procedures described in the following sections.

You can use the Object Browser to examine the properties and methods of the Word object models components. Press F2 in the Visual Basic window to open the Object Browser, and select Word in the Libraries drop-down list at the top-left of its window. Figure 6.5 shows the Object Browser with the MailMerge object selected in the Classes list, so you can examine its properties and methods.

The following sample procedures use Automation code to perform some common tasks when working with the Word object model.


Examining the Word MailMerge object in the Object Browser.


Examining the Word MailMerge object in the Object Browser.

0 0

Post a comment