Creating Objects

You can use early binding or late binding to create instances of Component Object Model (COM) objects so that you can use them in ATEasy applications. Using late binding, you create COM objects that are defined by a variable declaration of type Object. Using early binding, you create COM objects that are defined by a variable of a type that is specific to the object.

Creating Objects Using Late Binding

To create an instance of a COM object using late binding, first declare a variable (for example, a variable named "ob") and define its type as Object.

ob = CreateObject("Excel.Sheet")

Declaring a variable as an Object type creates a variable that can contain a reference to any type of object. However, access to the object through that variable is late-bound; that is, the binding occurs when your program is run.

The CreateObject function must provide the object's registered name or PROGID. The PROGID is the name of the object's class. In the example above, the PROGID is "Excel.Sheet." At run time, when ATEasy executes this statement, it locates the PROGID entry in the Windows registry, and thus gets access to the class information that it needs to create an instance of the object. The assignment operator ("=" in the example above) also queries the class to make sure that it supports the IDispatch interface, the COM interface supported by Object types. Once these processes have completed, ATEasy starts the application and creates the object, in this case an Excel spreadsheet. Once an object is created, you reference it in code using the object variable you defined.

Creating an Object Using Early Binding

To create an object variable that results in early binding, that is, binding when the program is compiled, declare the object variable with a specific class type. In order to use a specific class type, you must have added to your application a type library containing that class. For example, you can declare and create the following Microsoft Excel references:

First declare the variable named xlApp to be type Excel.Application. Declare the variable xlwbs as type Excel.Workbook.

xlApp = CreateObject("Excel.Application")

xlwbs = xlApp.Workbooks.Add

When an object, such as that returned by the Add method above, is assigned to an object variable of a specific type, such as xlwbs above, at run time the application calls the Query interface to verify that the object really is of the type declared. Using an object inside a type library through the early-bound variable can give better performance, but can refer only to the class specified in the declaration.

You can pass an object returned by the CreateObject function to a function expecting an object as an argument. For example, the following code creates and passes a reference to an Excel.Application object:

MySub(CreateObject("Excel.Application"))

Note: Use CreateObject when there is no current instance of the object. If an instance of the object is already running, a new instance is started and an object of the specified type is created. To use the current instance, or to start the application and have it load a file, use the GetObject function.

If an object has registered itself as a single-instance object, only one instance of the object is created and returned, no matter how many times CreateObject is executed.

For more information, see Getting Objects and CreateObject Procedure.