Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 22: Integrating Access with Other Applications


797


request for the “Outlook.Application” object to Windows, and Windows dynamically cre-
ates an instance of Outlook and provides the connection to Access.

Notice the quotes around “Outlook.Application”. This is not an object reference. Instead,
“Outlook.Application” is the name of an Automation server as it exists in the Windows reg-
istry. Windows looks up “Outlook.Application” in the registry, finds the path to the
Outlook executable, and performs the work of starting Outlook and connecting Outlook to Access.

An error is thrown if Outlook is not installed on the user’s computer. In this case, no reference to
Outlook exists in the system registry, and Windows can’t resolve the reference to “Outlook.
Application”.

Note
Did you notice that the code in this example does not specify a particular version of Outlook? One of the best
features of Automation is that Windows uses whichever version of the Automation server is installed on the
user’s computer. The Automation server reference in the system registry point to the latest version of the
server, without regard to the server’s version. The only time you may need to specify a version is when the
Automation code you’ve written uses features only available in a particular version of the server. To do this,
you might use “Outlook.Application.10” or “Outlook.Application.12.” Otherwise, keep references to
Automation servers as generic as possible and let Windows take care of the details.


Getting an existing object instance
Using the New keyword or the CreateObject function creates a new instance of the Automation
server (see “Creating an instance of an Automation object” section, earlier in this chapter). If you
don’t want a new instance of the server created each time you create an object, use the
GetObject function. The format of the GetObject function is as follows:

Set objectvariable = GetObject([pathname][, class])

In this statement, pathname refers to a file (like a Word document) that is associated with an
Automation server (in this case, Microsoft Word). For example, the following statement creates an
instance of Word and loads a Word document named MyDoc.doc into Word:

Dim ObjWord As Object
Set ObjWord = GetObject(“C:\MyDoc.doc”, )

Notice that you don’t have to provide a class name (“Word.Application”) because Windows is
able to resolve the file’s extension (.doc) to Microsoft Word. You’d need to provide the class name
if there were more than one application on the user’s computer that might be mapped to the file’s
extension.

The pathname parameter is optional. To use the pathname parameter, you specify a full path and
filename to an existing file for use with the Automation Server.
Free download pdf