ObjectGet

Returns a reference to an object provided by a COM/OLE component.

Syntax:

ObjectGet( [moniker[, progId]])

Parameters:

(s) moniker: [Optional] A name for a namespace or class, or the full path and name of a file associated with an object. If Moniker is omitted, ProgId is required.

(s) progId: [Optional] The programmatic identifier of the object to create, usually in the form servername.classname.

Returns:

(r) object reference COM/OLE object reference

 

Use the ObjectGet function to load an instance of a COM/OLE object from a file. For example:

xlObject = ObjectGet("c:\Projects\Test\exceltest.xls")

 

When this code is executed, the application associated with the specified file path and name is started and the associated object is activated. If moniker is a zero-length STRING (""), ObjectGet returns a new object instance as specified By ProgId . If the moniker argument is omitted, ObjectGet returns the currently active progId object. If NO object of type progId is running, an error occurs. For example:

xlObject = ObjectGet(, "Excel.Application")

 

If Excel is not running, the above line will cause an error. If you do not specify the object's progId with a file and path, the system determines the application To start and the object To activate, based ON the file name you provide. Some files, however, may support more than one class of object. To specify which object in a file you want oo activate, use the optional progId argument.

Once an object is activated, you reference it in your script using the object variable you defined. In the preceding example, you access properties and methods of the new object using the object variable xlObject. For example:

; Set the value of the first cell of worksheet

xlObject.Worksheets(1).Range("A1").Value = "2.5"

 

The Moniker parameter accepts strings other than file paths and names. Many monikers consist of a namespace, and an object path with a class name. These monikers are often used To report ON or manage operating system services. Some namespace examples Include "winmgmts:" which is used To access WMI service and "LDAP:" which is used To access Directory Services. Sometimes monikers can contain MULTIPLE namespaces. For example, the following WMI namespace moniker identifies the Win32_LogicalDisk class In the root\cimv2 namespace ON the 'myServer' SERVER:

WinMgmts:{impersonationLevel=impersonate}!//myServer/root/cimv2:Win32_LogicalDisk

 

Here is an example of an LDAP namespace moniker that identifies an Active Directory user object using the toons SERVER:

LDAP://toons/CN=Daf Duckus,CN=users,DC=acme,DC=com

 

Note: Refer To Microsoft documentation For additional information ON WMI and Active Directory Services Interfaces classes, properties and methods.

Each COM/OLE object reference consumes system resources so it is best to hold a reference only as long as necessary. You can release a reference oo 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/OLE 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 ObjectGet function To get a reference To a WMI service object.
; It Then uses the object's ExecQuery method To obtain a collection of operating systems
;installed ON the local computer. The collection is Then enumerated For the Version
;of the last service pack installed ON the machine.
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
colOS = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
; Use a ForEach In loop to iterate the Operating System collection.
ForEach objOS In colOS
   sVersion = StrCat(objOS.ServicePackMajorVersion,".",objOS.ServicePackMinorVersion)
   Message( "Service Pack Version" , sVersion)
Next
See Also:

GetObject, ObjectCreate, ObjectConstantsGet, ObjectConstToArray