WSH » WScript » GetObject

Syntax:
WScript.GetObject (strPathName [,strProgID] [,strSubPrefix])
strPathName
Receives the location of the file that will be loaded into the associated application.
strProgID
If the specified file can be handled by more than one application, this parameter accepts the ProgID of the particular application that you wish to use to load the file.
strSubPrefix
If non-empty, the CreateObject method will bind the events of the newly loaded object to event handlers beginning with the string passed to this parameter.

The GetObject method loads the specified file into the associated application and returns a reference to the instance of the application object. If an application object of the appropriate type already exists, the specified file will be loaded into the existing instance, otherwise a new instance will be created. If the specified file can be handled by multiple applications, the ProgID of the desired application can be specified. If an empty string is specified for the filename, a ProgID must be specified, and this method will behave the same as a call to CreateObject. The GetObject method can also be used to register the current script as an event-handler for the returned object.

The GetObject method can also be used to register the current script as an event-handler for the returned object by specifying a non-empty value for strSubPrefix. See the documentation on the ConnectObject and CreateObject methods for examples of handling events in your script.

Examples

Code:
Set objWord = WScript.GetObject("C:\test.doc")
objWord.Application.Visible = True
WScript.Echo "You just opened", objWord.Name
Output:
You just opened Test.doc
Explanation:

The use of the GetObject method is demonstrated in the this VBScript code which loads the file "C:\test.doc" into the associated application (Microsoft Word) and returns a reference to the instance of the Word Document object. It then makes sure that the containing Word Application is visible, and prints the name of the document just opened.

Language(s): VBScript

See Also: