To automate the objects in another application, you create an object variable referring to the target application or an object in the application. You can use early binding or late binding to establish the link between VBA and the other application's objects. Early binding requires that you establish a reference to the target application's type library, and you must declare any object variables that refer to the target objects using their correct type. If you declare the object variables as the generic Object type, VBA uses late binding.

Early binding produces code that executes faster than late binding, and you can get information on the target application's objects using the Object Browser and the shortcut tips that automatically appear as you type your code. Syntax and type checking is also performed as you code, so you are less likely to get errors when the code executes than with late binding, where these checks cannot be done until the code is run.

You must use the CreateObject or GetObject function to create an object variable reference to the target application when using late binding. You can use the same functions when early binding, but it is more efficient to use the New keyword. However, if you want to test for an open instance of another application at run time, GetObject can be usefully employed with early binding.

The techniques presented in this chapter allow you to create powerful programs that seamlessly tap into the unique abilities of different products. The user remains in a familiar environment such as Excel, while the code ranges across any product that has a type library and exposes its objects to VBA.

You need to be aware that virus writers can use the information presented here to wreak havoc on unprotected systems. Make sure that your system is adequately covered.

0 0

Post a comment