Article ID: 304661 - Last Review: January 17, 2007 - Revision: 8.4 How To Use Visual Basic .NET for Binding for Office Automation ServersThis article was previously published under Q304661 On This PageSUMMARY When you automate an application such as a Microsoft Office
application, the calls to the properties and methods of the Office
application's objects must be connected in some way to those objects. The
process of connecting property and method calls to the objects that implement
those properties and methods is commonly called binding. In Visual Basic .NET, the two types of binding that are
available are early binding and late binding. The type of binding you choose can affect many aspects of your
program, including performance, flexibility, and maintainability. This step-by-step article explains and compares early and late binding for Visual Basic .NET Automation clients and provides code samples that demonstrate both types of binding. Early BindingWith early binding, Visual Basic .NET uses type information that is available about the Office application in question to bind directly to the methods or properties that it needs to use. The compiler can perform type and syntax checks to ensure that the correct number and type of parameters are passed to the method or property, and that the returned value will be of the expected type. Because less work is required at run time to make a call to a property or method, early binding is sometimes faster; however, although early binding may be faster, performance differences when compared to late binding are often negligible.Early binding does have the minor disadvantage that it can introduce possible version compatibility issues. For example, suppose that an Automation server such as Microsoft Excel 2002 introduces a new method or property that was unavailable in Excel 2000, or makes a change to an existing property or method. These changes may alter the binary layout of the object and cause problems with a Visual Basic .NET application that uses the Excel 2002 type information to automate Excel 2000. To avoid this problem with early binding, it is generally recommended that you use the type information for the earliest version of the Office application that you wish to support when you develop and test your Automation client. The following steps demonstrate how to build an Automation client that uses early binding. Note that, as the steps illustrate, early binding requires you to reference the type library for the Automation client. Create an Automation Client That Uses Early Binding
Late BindingIn contrast to early binding, late binding waits until run time to bind property and method calls to their objects. To do this, the target object must implement a special COM interface: IDispatch. The IDispatch::GetIDsOfNames method allows Visual Basic .NET to interrogate an object about what methods and properties it supports, and the IDispatch::Invoke method then allows Visual Basic .NET to call those methods and properties. Late binding in this fashion has the advantage of removing some of the version dependencies that are inherent with early binding. However, late binding has the disadvantages of removing compile-time checks on the integrity of automation code, as well as not providing Intellisense features that can provide clues to correct calls to methods and properties.To use late binding in Visual Basic .NET, change the declarations of the objects in your automation code from their early bound types, such as Excel.Application to Object. Create an Automation Client That Uses Late Binding
REFERENCES For more information, visit the following Microsoft Web
site:
Microsoft Office Development with Visual Studio For
additional information about binding, click the article numbers below to view
the articles in the Microsoft Knowledge Base: http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx) 245115
(http://support.microsoft.com/kb/245115/EN-US/
)
INFO: Using Early Binding and Late Binding in Automation
244167
(http://support.microsoft.com/kb/244167/EN-US/
)
INFO: Writing Automation Clients for Multiple Office Versions
247579
(http://support.microsoft.com/kb/247579/EN-US/
)
INFO: Use DISPID Binding to Automate Office Applications Whenever Possible
APPLIES TO
| Article Translations
|
Back to the top
