How to: Use Visual C # .NET Bind Office Automation Server (from MSDN)

xiaoxiao2021-03-06  70

For Microsoft Visual Basic .NET versions of this article, see

304661.

This task content

Summary

Early binding

Create an advanced binding of automated clients that use early binding

Create an automation client with advanced binding

Summary When the application (such as Microsoft Office Application) is automatically run, the call to the attributes and methods of Office Applications must be connected to these objects in some way. The process of calling attributes and methods to connect to implementing these properties and methods are often referred to as

Bind. In Visual C #, there are two types of bindings, namely

Early binding and

The late binding. The selected binding type can affect many aspects of the program, including performance, flexibility, and maintainability.

This article describes and compares the early binding and late binding of the Visual C # automation client, providing code examples that describe these two categories of binding.

Back to top

Early Binding When earlier binding, Visual C # uses the method or attribute that is directly bound to what it needs to be used directly from the available type information of the Office application covered. The compiler can perform type and syntax check to ensure that the number and type of parameters pass to the method or attribute are correct, and the returned value is the desired type. Since early binding is small, the amount of work required to call attributes or methods is small, so sometimes the speed is faster. However, although early binding may be faster, the performance difference between advanced bindings is usually not large.

Early binding does have such a small disadvantage: the version compatibility problem may be brought. For example, assume that an automation server such as Microsoft Excel 2002 introduces a new method or attribute not not in Excel 2000, or changes existing attributes or methods. These changes may alter the binary layout of the object and cause the Excel 2000 type information to implement Excel 2000 automated Visual C # applications. To avoid such problems in early binding, you usually recommends that you use the type information of the Office application that you want to support when developing and testing the automated client.

The following steps describes how to create an automated client using an earlier binding. Note that as explained in these steps, early binding requires you to reference the type library of the automated client.

Back to top

Create an automation client using an earlier binding

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. 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, find the Microsoft Excel object library and click Select. Note: Office 2003 contains the main Interop assembly (PIA). Office XP does not contain PIA, but you can download PIA. For additional information about Office XP PIA, click the article number below to view the article in the Microsoft Knowledge Base: 328912 Info: Microsoft Office XP PIA is available for download in the Add Reference dialog box Click OK to accept your choice . If the system prompts you to generate a package for the selected library, click Yes. On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window of the form will appear. In the code window, put the following code private void button1_click (Object Sender, System.EventArgs E)

{

}

Replace with: Private Void Button1_Click (Object Sender, System.EventArgs E) {

Excel.Application Objapp;

Excel._Workbook Objbook;

Excel.Workbooks objBooks;

Excel.sheets objsheet;

Excel._Worksheet Objsheet;

Excel.range Range;

Try

{

// Instantiate Excel and Start a New Workbook.

Objapp = new excel.Application ();

ObjBooks = objapp.workbooks;

Objbook = objbooks.add (missing.value);

Objsheets = objbook.worksheet;

Objsheet = (Excel._Worksheet) objsheets.get_Item (1);

Range = objsheet.get_range ("a1", missing.value;

Range.set_Value (Missing.Value, "Hello, World!");

// Return Control of Excel to the user.

Objapp.visible = true;

Objapp.userControl = true;

}

Catch (Exception theexception)

{

String ErrorMessage;

ErrorMessage = "Error:";

ErrorMessage = String.concat (ErrorMessage, theexception.Message);

ErrorMessage = String.concat (ErrorMessage, "Line:");

ErrorMessage = String.concat (ErrorMessage, theexception.Source);

MessageBox.show (ErrorMessage, "Error");

}

}

Scroll to the top of the code window. Add the following code line to the end of the Using instruction list: use system.reflection;

Using Excel = Microsoft.Office.Interop.Excel;

Back to top

The late binding is different from early binding, and the attributes and methods will be bundled to their objects when the late binding will wait until running. To this end, the target object must realize a special COM interface:

Idispatch. use

Idispatch :: getidsofnames method, Visual C # can ask for what methods and properties supporting objects, then,

Idispatch :: Invoke method allows Visual C # to call these methods and properties. The advantage of this late binding is that it eliminates some of the earlier bindings inherent. However, it also has the following disadvantages: omitting compilation of automated code integrity, nor does it provide "Intelligent Performance" function (this feature provides a prompt to help correctly call methods and properties).

To use advanced bindings in Visual C #, please use

System.Type.InvokeMember method. This method is called

Idispatch :: getidsofnames and

Idispatch :: Invoke to bind to the method and properties of the automation server.

Back to top

Create an automation client using advanced binding

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select a Windows application from the Visual C # project type. FORM1 is created by default. On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window of the form will appear. In the code window, pull the following code private void button1_click (Object Sender, System.EventArgs E) {

}

Replace with: Private Void Button1_Click (Object Sender, System.EventArgs E)

{

Object objapp_ilate;

Object objbook_late;

Object objBooks_late;

Object objsheets_late;

Object objsheet_late;

Object Objrange_late;

Object [] Parameters;

Try

{

// Instantiate Excel.

ObjApp_late = (object) new excel.application ();

// Get the workbooks collection.

ObjBooks_Late = Objapp_late.gettype (). InvokeMember ("Workbooks",

BindingFlags.getProperty, NULL, OBJAPP_LATE, NULL);

// Add a new workbook.

Objbook_late.gettype (). InvokeMember ("add",

Bindingflags.InvokeMethod, null, objBooks_late, null;

// Get the worksheets collection.

Objsheets_late = Objbook_late.gettype (). InvokeMember ("Worksheets",

BindingFlags.getProperty, Null, ObjBook_late, NULL);

// Get the first worksheet.

Parameters = New Object [1];

Parameters [0] = 1;

Objsheet_late = objsheets_late.gettype (). InvokeMember ("item",

BindingFlags.getProperty, Null, Objsheets_late, parameters;

// Get a Range Object That Contains Cell A1.

Parameters = New Object [2];

Parameters [0] = "a1";

Parameters [1] = missing.value;

ObjRange_late = objsheet_late.gettype (). InvokeMember ("Range",

Bindingflags.getProperty, Null, Objsheet_late, parameters;

// Write "Hello, World!" IN Cell A1.

Parameters = New Object [1];

Parameters [0] = "Hello, World!";

Objrange_late.gettype (). InvokeMember ("Value", BindingFlags.SetProperty, Null, ObjRange_late, Parameters;

// Return Control of Excel to the user.

Parameters = New Object [1];

Parameters [0] = True;

Objapp_late.gettype (). InvokeMember ("visible", bindingflags.setproperty,

null, objapp_late, parameters;

Objapp_late.gettype (). InvokeMember ("UserControl", BindingFlags.SetProperty,

null, objapp_late, parameters;

}

Catch (Exception theexception)

{

String ErrorMessage;

ErrorMessage = "Error:";

ErrorMessage = String.concat (ErrorMessage, theexception.Message);

ErrorMessage = String.concat (ErrorMessage, "Line:");

ErrorMessage = String.concat (ErrorMessage, theexception.Source);

MessageBox.show (ErrorMessage, "Error");

}

}

Scroll to the top of the code window. Add the following code line to the end of the Using instruction list: use system.reflection;

Back to top

Refer to more information, visit the following Microsoft Developer Network (MSDN) Web site:

Microsoft Office Development with Visual Studio (using Visual Studio) http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficeDev.asp For additional information about binding, click below Article number to see the article in the Microsoft Knowledge Base:

245115 Info: Using Early Binding and Late Binding in Automation

244167 Info: Writing Automation Clients for Multiple Office Versions

247579 Info: Use Dispid Binding to Automate Office Applications WHENEVER POSSIBLE

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft Office Access 2003 Microsoft Access 2002 Microsoft Office Excel 2003 Microsoft Excel 2002 Microsoft Office PowerPoint 2003 Microsoft PowerPoint 2002 Microsoft Office Word 2003 Microsoft Word 2002

转载请注明原文地址:https://www.9cbs.com/read-109008.html

New Post(0)