GetObject (function)

Syntax

GetObject(filename$ [,class$])

Description

Returns the object specified by filename$ or returns a previously instantiated object of the given class$.

Comments

This function is used to retrieve an existing OLE automation object, either one that comes from a file or one that has previously been instantiated.

 

The filename$ argument specifies the full pathname of the file containing the object to be activated. The application associated with the file is determined by OLE at runtime. For example, suppose that a file called c:\docs\resume.doc was created by a word processor called wordproc.exe. The following statement would invoke wordproc.exe, load the file called c:\docs\resume.doc, and assign that object to a variable:

  Dim doc As Object
  Set doc = GetObject("c:\docs\resume.doc")

 

To activate a part of an object, add an exclamation point to the filename followed by a string representing the part of the object that you want to activate. For example, to activate the first three pages of the document in the previous example:

  Dim doc As Object
  Set doc = GetObject("c:\docs\resume.doc!P1-P3")

 

The GetObject function behaves differently depending on whether the first parameter is omitted. The following table summarizes the different behaviors of GetObject:

 

Filename$

Class$

GetObject Returns

 

Omitted

Specified

Reference to an existing instance of the specified object. A runtime error results if the object is not already loaded.

 

""

Specified

Reference to a new object (as specified by class$). A runtime error occurs if an object of the specified class cannot be found.

This is the same as CreateObject.

 

Specified

Omitted

Default object from filename$. The application to activate is determined by OLE based on the given filename.

 

Specified

Specified

Object given by class$ from the file given by filename$. A runtime error occurs if an object of the given class cannot be found in the given file.

Example

This first example instantiates the existing copy of Excel.

Sub Main()
  Dim Excel As Object
  Set Excel = GetObject(,"Excel.Application")

This second example loads the OLE server associated with a document.

  Dim MyObject As Object
  Set MyObject = GetObject("c:\documents\resume.doc")
End Sub

See Also

CreateObject (function); Object (data type).

More information

G