GetObject Function

See Also52QYZ3W

Retrieves an OLE Automation object from a file.


GetObject (filename [, class ])


The GetObject function has these parts:

Part               Description


filename         The full path and name of the file containing the object to retrieve.  If filename is an empty string (""), then class is required.

class              A string representing the class of the object to retrieve.


The class argument is optional.  It uses the following syntax:


The class argument has these parts:

Part               Description


appname        The name of the application providing the object.

objecttype      The type or class of object to create.


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

Dim MyObject As Object
Set MyObject = GetObject("C:\WORDPROC\DOCS\OLETEST.DOC")


When this code is executed, the application associated with the specified file name is started (WORDPROC.EXE in this example) and the object in the specified file is activated.

If the filename argument is set to an empty string (""), this function returns the currently active object of the specified type.  If there is no object of that type active, 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 many spreadsheet applications you specify the rows and columns of a range of cells using an R1C1 syntax.  You could use the following code to activate a range of cells within a spreadsheet called REVENUE.SPD:

Set Sheet = GetObject("C:\ACCOUNTS\REVENUE.SPD!R1C1:R10C20")


If you do not specify the object's class, the OLE2 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 spreadsheet might support three different types 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 in a file you want to activate, use the optional class argument.  For example:



In the above example, SPDSHEET is the name of a spreadsheet application and WORKSHEET one of the object types it supports.

Once an object is activated, you reference it in Visual Basic 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:

Sheet.Row      = 4
Sheet.Column   = 2
Sheet.Insert   = "Hello, world."

See Also


CreateObject Function0M4P7F

Object Property1MHI551

OLE Automation1J3AYU5


Set StatementLANSET


Programmer's Guide:

Chapter 23, "Programming Other Applications' Objects"