Using the Get Object Function

If you are certain that the Automation object already exists or is already open, consider using the GetObject function. This function looks like this:

GetObject([pathname][, class])

The GetObject function has two arguments, both of which are optional. Use the first argument to specify the name of the file that you want to open. The full path should be given. If you omit this argument, you have to specify the class argument that indicates the type of object to work with. For example:

Excel.Application

Excel.Sheet

Excel.Chart

Excel.Range

Word.Application

Word.Document

PowerPoint.Application

To create an Excel object based on the Invite.xls spreadsheet and force the object to be an Excel 5 spreadsheet, you could use the following declaration:

' late binding

Dim excelObj As Object

Set excelObj = GetObject("C:\Invite.xls", Excel.Sheet.5") To set the object variable to a specific Word document, you would use:

'early binding

Dim wordObj As Word.Application

Set wordObj = GetObject("C:\Invite.doc")

To access a running Office application object, leave the first argument out: Dim excelObj As Object

Set excelObj = GetObject(, "Excel.Application")

When the GetObject function is called without the first argument, it returns a reference to an instance of the application. If the application isn't running, an error will occur.

0 0

Post a comment