Activating an Object from a File

Many Automation applications let the user save objects in files. For example, a spreadsheet application that supports Worksheet objects lets the user save the worksheet in a file. The same application may also support a Chart object that the user can save in a file.

To activate an object from a file, first declare an object variable, and then call the GetObject function using the following syntax:

GetObject (filename[, ProgID])

The filename argument is a string containing the full path and name of the file to be activated. For example, an application named SpdSheet.exe creates an object that was saved in a file named Revenue.spd. The following code invokes Spdsheet.exe, loads the file Revenue.spd, and assigns Revenue.spd to an object variable:

Dim Ss As Spreadsheet
Set Ss = GetObject("C:\Accounts\Revenue.spd")

If the filename argument is omitted, then GetObject returns the currently active object of the specified ProgID. For example:

Set Ss = GetObject (,"SpdSheet.Application")

If there is no active object of the class SpdSheet.Application, an error will occur.

In addition to activating an entire file, some applications let you activate part of a file. To activate part of a file, add an exclamation point (!) or a backslash (\) to the end of the file name, followed by a string that identifies the part of the file you want to activate. For information on how to create this string, refer to the object's documentation.

For example, if SpdSheet.exe is a spreadsheet application that uses R1C1 syntax, the following code could be used to activate a range of cells within Revenue.spd:

Set Ss = GetObject("C:\Accounts\Revenue.spd!R1C1:R10C20")

These examples invoke an application and activate an object. In these examples, the application name (SpdSheet.exe) is never specified. When GetObject is used to activate an object, the registry files determine the application to invoke and the object to activate based on the file name or ProgID that is provided. If a ProgID is not provided, Automation activates the default object of the specified file.

Some ActiveX components, however, support more than one class of object. Suppose the spreadsheet file, Revenue.spd, supports three different classes of objects: an Application object, a Worksheet object, and a Toolbar object, all of which are part of the same file. To specify which object to activate, an argument must be supplied for the optional ProgID parameter. For example:

Set Ss = GetObject("C:\Revenue.spd", "SpdSheet.Toolbar")

This statement activates the SpdSheet.Toolbar object in the file Revenue.spd.