GetObject Function

Description

Retrieves an OLE Automation object from a file.

Syntax

GetObject([pathname][,class])

The GetObject function syntax has these named-argument parts:

Part

Description

pathname

The full path and name of the file containing the object to retrieve. If pathname is omitted, class is required.

class

A string representing the class of the object.


The class argument uses the syntax: "appname.objecttype" and has these parts:

Part

Description

appname

The name of the application providing the object.

objecttype

The type or class of object to create.


Remarks

Note

If an application which supports OLE Automation exposes an object library, it is preferable to use the functions defined within the library for object access rather than use GetObject.

Use the GetObject function to access an OLE Automation object from a file and assign the object to an object variable. To do this, use the Set statement to assign the object returned by GetObject to the object variable. For example:


Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

When this code is executed, the application associated with the specified file name is started and the object in the specified file is activated.

If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted entirely, GetObject returns the currently active object of the specified type. If no object of the specified type exists, an error occurs.

The above example shows how to activate an entire file. However, some applications allow you to activate part of a file. To do this, add an exclamation point (!) 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, see the documentation for the application that created the object.

For example, in a drawing application you might have multiple layers to a drawing stored in a file. You could use the following code to activate a layer within a drawing called SCHEMA.CAD:


Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")

If you do not specify the object's class, the OLE.DLLs determine the application to invoke and the object to activate based on the file name you provide. Some files, however, may support more than one class of object. For example, a drawing might support three different types of objects: an application object, a drawing object, and a toolbar object, all of which are part of the same file. To specify which object in a file you want to activate, use the optional class argument. For example:


Set MyObject = GetObject("C:\DRAWINGS\SAMPLE.DRW", "FIGMENT.DRAWING")

In the above example, FIGMENT is the name of a drawing application and DRAWING is one of the object types it supports.

Once an object is activated, you reference it in code using the object variable you defined. In the above example, you access properties and methods of the new object using the object variable MyObject. For example:


MyObject.Line 9, 90
MyObject.InsertText 9, 100, "Hello, world."
MyObject.SaveAs "C:\DRAWINGS\SAMPLE.DRW"

See Also

CreateObject Function, Set Statement.