Wscript.GetObject

The GetObject method retrieves an Automation object from a file, or an object specified by strProgID parameter. 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 there is no current instance and you don't want the object started from a file that is already loaded, use the CreateObject method.

Syntax

Wscript.GetObject(strPathname [,strProgID] ], [strPrefix]) = objObject
 

Parameters

strPathname
The full path and the name of the file containing the object to retrieve. The strPathname parameter is required.
strProgID
A string representing the program identifier (ProgID) of the object.
strPrefix
If the strPrefix parameter is specified, Windows Scripting Host connects the object's outgoing interface to the script file after creating the object. When the object fires an event, Windows Scripting Host calls a subroutine named strPrefix and the event name.

For example, if strPrefix is "MYOBJ_" and the object fires an event named "OnBegin", Windows Scripting Host calls the "MYOBJ_OnBegin" subroutine located in the script.

Return

objObject
The Automation object retrieved.

Remarks

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 can't use GetObject to obtain a reference to a 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.

Examples

Dim MyObject As Object
    Set MyObject = GetObject(""C:\CAD\SCHEMA.CAD"")
    = MyCAD.Application
 

When this 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.

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 you might have multiple layers of a drawing stored in a file. 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. To specify which object in a file you want to activate, use the optional class parameter. For example:

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

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

After 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"
 

See Also

Wscript.CreateObject method, Wscript.DisconnectObject method