Accessing COM Objects |
|
You can manipulate other applications' COM objects directly by first opening the object with the ObjectGet and ObjectCreate functions.
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:
objxl = ObjectCreate ("Excel.Application")
starts Microsoft Excel. The object returned to the variable 'objxl' is a reference to the Excel.Application object just created. In other words, an object handle.
ObjectGet is used with an application that's already running, or to start an application with a file already loaded. For example:
objxlbook = ObjectGet ("C:\TEST.XLS")
would start Microsoft Excel with the file 'Test.xls' already loaded. The object returned to the variable 'objxlbook' would be a reference to the Workbook object representing the just opened 'Test.xls' file.
The ObjectCreate function is used to open the object. This function accepts a string (ProgId) that indicates the application name and the type of object you want to create. Use the following syntax to specify an object to create:
Application.ObjectType
For example, let's say there is a orgchart application named ORGCHART.EXE that supports a single object: an orgchart. Furthermore, the OrgChart object supports two sub-objects: a box and a line. The object might be defined as:
OrgChart.Chart
Once you know the type of object you want to create, you use the ObjectCreate function to create the object. Capture the value returned by the ObjectCreate function to a variable. Here's an example:
objchart = ObjectCreate("OrgChart.Chart")
Once you have the primary object in hand - in the 'objchart' variable in this case, you can create the sub-objects and assign them to their own variables.
objtopbox = objchart.NewBox
objbottombox = objchart.NewBox
objline = objchart.NewLine
When this code executes, the application providing the object is started (if it is not already running) and an object is created. The object belongs to the application that created it. This object can be referenced using the variable you placed the return value of the ObjectCreate function. For example, after creating the object, you could write code to open sub-objects, change the background color, set a default font, set a title, and save the object to a file:
objchart.Color = "White"
objchart.FontName = "Arial"
objchart.FontSize = 12
objchart.Title = "Deana’s Org Chart"
objtopbox.Position(2,2)
objtopbox.Text = "The Boss"
objbottombox.Position(2,8)
objbottombox.Text = "Deana"
objline.Begin(2,2)
objline.End(2,8)
objchart.SaveAs("C:\ORGCHART\DEANA.ORG")
When creating an object, some applications require that the application providing the object is either active or on the system's path.
When you are through with an object, set the object equal to a null string ("") or to 0 (zero) to tell the WIL processor that you are done with the object.
objline = ""
objtopbox = ""
objbottombox = ""
objchart = ""
To get a list of objects that an application supports, you must consult that application's documentation. It may also help to poke around in the Windows registry. For example check out the HKEY_CLASSES_ROOT key. Be aware, though, that intentional, unintentional, or accidental changes to the registry may completely destroy a Windows installation and require a complete re-installation of ALL your software to recover.
See Also: