Part III: More-Advanced Access Techniques
798
Note
Have you noticed that GetObject is an example of late binding? Access doesn’t care which application is
going to open the file passed as the pathname argument, and Windows does all the work, in any case.
Using GetObject to instantiate an Automation server is a convenient way to open the server on a
particular file. However, this statement will fail if the file specified by the pathname argument
doesn’t exist, or if the filename extension is omitted or not mapped to an application on the user’s
computer.
The class argument is optional, but it’s used to clarify to Windows exactly which Automation
server to instantiate and open. Consider a file such as MyFile.txt. Although Notepad is the
default text editor on most computers, you might want to specify Word as the Automation server:
Dim ObjWord As Object
Set ObjWord = GetObject(“C:\MyText.txt”, “Word.Application”)
Using late binding through the GetObject function is a convenient way to open Automation
servers on arbitrary files. Consider an Access application that contains a list of different files,
including Word and Excel file, images in .jpg and .tiff format, and .mpg and .avi video
clips. Writing Automation code to work with all these file types could be a daunting task, but writ-
ing a few lines of code using late binding greatly simplifies the task.
Note
The specified document is opened in the server application. Even if you omit the parameter, you must still
include the comma (,).
The class parameter is the same parameter that’s used with the CreateObject function. See
Table 22.1 for a list of some class arguments used in Microsoft Office.
TABLE 22.1
Class Arguments for Common Offi ce Components
Component Class Argument Object Returned
Access Access.Application Microsoft Access Application object
Excel Excel.Application Microsoft Excel Application object
Excel Excel.Sheet Microsoft Excel Workbook object
Excel Excel.Chart Microsoft Excel Chart object
Word Word.Application Microsoft Word Application object
Word Word.Document Microsoft Word Document object