ObjectCreate

Creates and returns a reference to a COM object.

Syntax:

ObjectCreate( progid [, location])

Parameters

(s) progid: The programmatic identifier of the object to create, usually in the form servername.classname.

(s) location: [Optional] The name of the network server where the object will be created. If location is an empty string ("") or omitted, the local machine is used.

Returns:

(r) object reference COM object reference

 

To create an instance of a COM object, assign the return value of ObjectCreate to a variable:

adoApp = ObjectCreate("ADODB.Connection")

 

As shown in the examples that follow, you can access properties and methods of the object using the object variable and dot (.) notation.

You can create an object on a remote networked computer by passing the name of the computer to the Location argument of the ObjectCreate function. That name is the same as the Machine Name portion of a share name. i.e.: for a share named\\MyServer\Public, the Location is "MyServer."

The following code returns the version number of an instance of Excel running on a remote computer named MyServer:

; Replace string "\\MyServer" with name of the remote computer.

xlObj = ObjectCreate("Excel.Application", "\\MyServer")

message("Remote Excel version", xlObj.Version)

xlObj = ""

 

Note: Refer to Microsoft documentation for additional information on making an application accessible on a remote networked computer.

Use ObjectCreate when there is no current instance of the object. If, however, an instance of the object is already running, ObjectCreate starts a new instance, and an object reference of the specified type is returned. To use the current instance or to start the application and have it load a file, or to access an object using a moniker, use the ObjectGet function. If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times ObjectCreate is executed.

Each COM object reference consumes system resources so it is best to hold a reference only as long as necessary. You can release a reference to an object by simply assigning a new value to the object variable. Some object servers require that a termination method be called before an object’s last reference is released. Check your COM server documentation for details.

Note: The legacy function ObjectClose can still be used to release an object reference. However, continued use of the function is discouraged.

ObjectCreate vs. ObjectGet: What's the Difference?

Besides minor differences in the syntax, the ObjectCreate and ObjectGet functions are used in different contexts. The differences can be summarized as follows:

ObjectCreate is used to create an interface to a new instance of an application. Use ObjectCreate when it's not certain whether the application to integrate is running. For example:

 

xl = ObjectCreate ("Excel.Application")

 

starts Microsoft Excel. The object returned In xl is a reference to the Excel.Application object just created.

ObjectGet is used with an application that's already running, or to start an application with a file already loaded. For example:

 

xlBook = ObjectGet ("C:\TEST.XLS")

 

would start Microsoft Excel with the file Test.xls already loaded. The object returned would be a reference To the Workbook object representing the just opened Test.xls file.

Example:
; This example uses the ObjectCreate function to create a
; Microsoft Internet Explorer window and Display a Web page.
; to use this example, Internet Explorer must be installed on
; the computer where this program will run.
objExplorer = ObjectCreate("InternetExplorer.Application")
objExplorer.addressbar = @FALSE
objExplorer.statusbar = @FALSE
objExplorer.menubar = @FALSE
objExplorer.toolbar = @FALSE
objExplorer.visible = @TRUE
objExplorer.Width = 800
objExplorer.Height = 550
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.navigate("http://www.winbatch.com/")
; Wait until page loads...
While objExplorer.busy || objExplorer.readystate<> 4
   TimeDelay(0.5)
EndWhile
; Do something.
objExplorer.quit
objExplorer = ""
See Also:

ObjectGet, CreateObject, ObjectConstantsGet, ObjectConstToArray