Microsoft® Windows® Script Host
GetObject Method
WSH Reference
Version 1

See Also                      Applies To


Description
Retrieves an Automation object from a file or an object specified by the strProgID parameter.
Syntax
object.GetObject(strPathname [,strProgID], [strPrefix])
Parameters
Part Description
object WScript object.
strPathname Required. The full path and name of the file containing the object to retrieve. is required.
strProgID Optional. A string representing the object's program identifier (ProgID).
strPrefix Optional. If the parameter strPrefix is specified, Windows Script Host connects the object's outgoing interface to the script file after creating the object. When the object fires an event, Windows Script Host calls a subroutine whose name consists of strPrefix and the event name.
For example, if strPrefix is "MYOBJ_" and the object fires an event named "OnBegin," Windows Script Host calls the "MYOBJ_OnBegin" subroutine located in the script.
Remarks
Use the GetObject method when there is a current instance of the object or if you want to create the object from a file that is already loaded. If no current instance exists and if you do not want the object started from a file that is already loaded, use the CreateObject method.

If an object has registered itself as a single-instance object (for example, the Word.Basic object in Microsoft Word 7.0), only one instance of the object is created, no matter how many times CreateObject is executed. In addition, 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 path parameter is omitted. You cannot use GetObject to obtain a reference to a Microsoft Visual Basic® class created with Visual Basic 4.0 or earlier.

GetObject works with all COM classes, independent of the language used to create the object.

Example
When the following code is executed, the application associated with the specified strPathname is started, and the object in the specified file is activated. If strPathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the strPathname parameter is omitted entirely, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

Dim MyObject As Object
Set MyObject = GetObject("C:\CAD\SCHEMA.CAD")
MyApp = MyObject.Application
Some applications allow you to activate part of a file. To do this, 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, a drawing stored in a file might have multiple layers. You could use the following code to activate a layer within a drawing file called schema.cad:

Set LayerObject = GetObject("C:\CAD\SCHEMA.CAD!Layer3")
If you do not specify the object's class, COM determines the application to start and the object to activate according to 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.

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

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