Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 22: Integrating Access with Other Applications


795


Dim WordObj As Object
Set WordObj = CreateObject(“Word.Application”)

Tip
If you create an object for an application that isn’t referenced, an IntelliSense drop-down, such as the ones
shown in Figures 22.2 and 22.3, will not appear.


Figure 22.2 shows the IntelliSense drop-down that appears immediately after you type New in the
Dim statement. At this point, you can select one of the Application object name types dis-
played (such as Word) or enter a the name of a referenced object library.

In Figure 22.2, IntelliSense shows Word, which is a library, as indicated by the books icon in the
Auto List Members list. This icon distinguishes a library object from a simple class object, such as
VBProject. Object libraries contain classes, whereas simple classes contain properties, methods,
and events. The class objects you see in Figure 22.2 (VBComponent, VBComponents,
VBProject, and so on) are included within other libraries referenced by the Chapter22.
accdb application.

Figure 22.3 shows the IntelliSense drop-down list that appears when you type a period after Word.
This drop-down list displays all the objects exposed in the Word object libary. In this case, clicking
the Application object completes the VBA statement.

Creating an instance of an Automation object
To perform an Automation operation, the operating system needs to start the application — if it
isn’t already started — and obtain a reference, or handle, to it. (Most Automation servers, like
Word, expose an Application object.) The Application object exists at the top of the object
library’s object hierarchy and often contains many subordinate objects, as well.

Creating a new instance with New
Once an object variable is declared, you create an instance of the object with the New keyword. In
the examples shown in Figures 22.2 and 22.3, the variable WordObj is set to a new instance of
Word’s Application object.

Caution
If you don’t create a reference to the Automation server with the References dialog box, VBA doesn’t recog-
nize the variable’s object type and generates an error on compile.


Every time you create an instance of an Automation server with the New keyword, a new instance
of the application is started. If you don’t want to start a new instance of the application, use the
GetObject function (see the “Getting an existing object instance” section, later in this chapter).

Note
Not all Automation servers support the New keyword. Consult the specific Automation server’s documentation
to determine whether it supports the New keyword. If the New keyword is not supported, you must use the
CreateObject function, which is discussed in the next section, to create an instance of the Automation server.

Free download pdf