Establishing the Connection

Once you have made a connection with an Office application, its objects are exposed for automation through a type library. There are two ways to establish such a connection: late binding and early binding. In either case, you establish the connection by creating an object variable that refers to the target application, or a specific object in the target application. You can then proceed to use the properties and methods of the object referred to by the object variable.

In late binding, you create an object that refers to the Office application before you make a link to the Office application's type library. In earlier versions of the Office applications, it was necessary to use late binding, and you will still see it used because it has some advantages over early binding. One advantage is that you can write code that can detect the presence or absence of the required type library on the PC running your code, and link to different versions of applications based on decisions made as the code executes.

Late binding is very useful when you have to run an application under different versions of Office. Office happily upgrades an application's early bound links when those links are to an earlier version. Unfortunately, the opposite is not true. An application linked to a later version of Office than the version it is running under gives errors. The problem is compounded if users can save the changes to the links in applications on a network. Because late binding does not create the links until execution time, the links are always to the version it is running under.

The disadvantage of late binding is that the type library for the target application is not accessed when you are writing your code. Therefore, you get no help information regarding the application, you cannot reference the intrinsic constants in the application, and when the code is compiled, the references to the target application may not be correct, because they cannot be checked. The links are only fully resolved when you try to execute the code, and this takes time. It is also possible that coding errors may be detected at this point that cause your program to fail.

Early binding is supported by all the Office applications, from Office 97 onward. Code that uses early binding executes faster than code using late binding, because the target application's type library is present when you write your code. Therefore, more syntax and type checking can be performed, and more linkage details can be established, before the code executes.

It is also easier to write code for early binding because you can see the objects, methods, and properties of the target application in the Object Browser, and as you write your code, you will see automatic tips appear, such as a list of related properties and methods after you type an object reference. You can also use the intrinsic constants defined in the target application.

In the following examples that involve Outlook, you could be interrupted by your virus scanning software when accessing Outlook, because the scanner could identify your actions as possible virus activity.

0 0

Post a comment