GetObject Function

Purpose

Returns a reference to an OLE object.

Syntax

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

object:Object exp

Description

The GetObject function accesses an OLE object from a file and assign the object to an object variable. Use the Set statement to assign the object returned by GetObject to the object variable. For example:

Global testobj As Object

Set testobj = GetObject("<path + file_name>", "program_name.object")

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

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

Some applications allow you to activate part of a file. Add an exclamation point (!) to the end of the file name and follow it with 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 don't specify the object's class, Automation determines the application to start 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:

Dim MyObject As Object

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

In the 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 preceding 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"

Note   Use the GetObject function when there is a current instance of the object or if you want to create the object with a file already loaded. If there is no current instance, and you don't want the object started with a file loaded, use the CreateObject function.

If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed. With a single-instance object, GetObject always returns the same instance when called with the zero-length string ("") syntax, and it causes an error if the pathname argument is omitted. You can't use GetObject to obtain a reference to a class created with GFA-BASIC 32.

Example

Using GetObject with Excel objects

Dim x As Object

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

' Excel must be running, otherwise an OLE Automation error occurs.

' The x refers to Excel.Application for the youngest instance of Excel.

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

' Behaves like: Set x = CreateObject("Excel.Application").

Set x = GetObject("", "Excel.Sheet")

' Behaves like: Set x = CreateObject("Excel.Sheet")

Set x = GetObject("c:\Excel\test.xls")

Set x = GetObject("c:\Excel\test.xls", "Excel.Sheet")

' Each of these starts an invisible reference-independent

' instance of Excel, if it's not running,

' otherwise it uses the youngest existing instance.

' If the specified XLS file is not open, then it

' is opened as a hidden workbook, and is

' reference-dependent unless the command was

' executed as an Excel command. The x refers

' to the activesheet in the specified file.

Set s = GetObject("", "Excel.Chart")

' Behaves like: Set s = CreateObject("Excel.Chart")

Set s = GetObject("c:\Excel\test.xls", "Excel.Chart")

' Behaves like: GetObject("c:\Excel\test.xls")

' except that the workbook must contain at least one chart sheet.

Remarks

These are ILLEGAL :

Set s = GetObject(, "Excel.Sheet")

Set s = GetObject("c:\Excel\test.xls", "Excel.Application")

The built-in API function GetObject is renamed in GetGdiObject or apiGetObject.

See Also

Automation, CreateObject

{Created by Sjouke Hamstra; Last updated: 07/10/2014 by James Gaite}