Microsoft KB Archive/304661

= How To Use Visual Basic .NET for Binding for Office Automation Servers =

Article ID: 304661

Article Last Modified on 1/17/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Office PowerPoint 2003
 * Microsoft PowerPoint 2002 Standard Edition
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q304661



For a Microsoft Visual C# .NET version of this article, see 302902.

IN THIS TASK

 * SUMMARY
 * Early Binding
 * Create an Automation Client That Uses Early Binding
 * Late Binding
 * Create an Automation Client that Uses Late Binding
 * REFERENCES



SUMMARY
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.

back to the top

Early Binding
With 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.

back to the top

Create an Automation Client That Uses Early Binding
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Select Windows Application from the Visual Basic .NET Projects types. Form1 is created by default. Add a reference to the Microsoft Excel Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate theMicrosoft Excel Object Library, and then click Select.

Note Office 2003 includes Primary Interop Assemblies (PIAs). Office XP does not include PIAs, but they can be downloaded. For additional information about Office XP PIAs, click the article number below to view the article in the Microsoft Knowledge Base:

328912 INFO: Microsoft Office XP PIAs Are Available for Download

 Click OK in the Add References dialog box to accept your selections.  On the View menu, select Toolbox to display the Toolbox, and add a button to Form1.</li> Double-click Button1. The code window for the Form appears.</li>  In the code window, replace the following code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub with: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim objApp As Excel.Application Dim objBook As Excel._Workbook Dim objBooks As Excel.Workbooks Dim objSheets As Excel.Sheets Dim objSheet As Excel._Worksheet Dim objRange As Excel.Range

' Instantiate Excel and start a new workbook. objApp = New Excel.Application objBooks = objApp.Workbooks objBook = objBooks.Add objSheets = objBook.Worksheets objSheet = objSheets.Item(1)

objRange = objSheet.Range(&quot;A1&quot;)

'Set the range value. objRange.Value(Excel.XlRangeValueDataType.xlRangeValueDefault) = &quot;Hello, World!&quot;

'Return control of Excel to the user. objApp.Visible = True objApp.UserControl = True End Sub </li>  Add the following directive at the top of code window: Imports Excel = Microsoft.Office.Interop.Excel </li></ol>

back to the top

Late Binding
In 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.

back to the top

Create an Automation Client That Uses Late Binding
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Select Windows Application from the Visual Basic .NET Projects types. Form1 is created by default.</li> On the View menu, select Toolbox to display the Toolbox, and add a button to Form1.</li> Double-click Button1. The code window for the Form appears.</li>  In the code window, replace the following code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub with: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim objApp As Object Dim objBook As Object Dim objBooks As Object Dim objSheets As Object Dim objSheet As Object Dim range As Object

' Instantiate Excel and start a new workbook. objApp = CreateObject(&quot;Excel.Application&quot;) objBooks = objApp.Workbooks objBook = objBooks.Add objSheets = objBook.Worksheets objSheet = objSheets.Item(1)

range = objSheet.Range(&quot;A1&quot;)

'Set the range value. range.Value = &quot;Hello, World!&quot;

'Return control of Excel to the user. objApp.Visible = True objApp.UserControl = True End Sub </li></ol>

back to the top

<div class="references_section">