ErrorHandler:
If Err = 429 Then
‘Word is not running; open Word with CreateObject
Set appWord = CreateObject(Class:=”Word.Application”)
Resume Next
Else
MsgBox “Error No: “ & Err.Number _
& “; 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 CreateObjector GetObjectfunction.
Although you use CreateObjector GetObjectto 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 appWordvariable 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 per-
haps 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
model’s 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.
NOTENOTE
Working with Word Documents and Templates 6