Creating a Reference to an Automation Library


To use the objects of a program that supports automation (a server), you have to reference the program's type library. A program's type library (also called its object library) is a file containing a description of the program's object model. After you've referenced the type library of an automation server (also called a component), you can access the objects of the server as though they were internal Visual C# objects.

You create the reference to Excel's automation library much like you created a reference to the System.Data namespace in Hour 21, "Working with a Database." To create a reference to the Excel library, follow these steps:

1.

Display the Add Reference dialog box by choosing Project, Add Reference.

2.

Click the COM tab to display the available COM components (programs that have a type library) on your computer.

3.

Scroll the list and locate the Microsoft Excel 11 Object Library (see Figure 22.1). Double-click the Excel item to create a reference to it and close the dialog box.



Figure 22.1. Visual C# .NET projects can use COM objects created by other tools.


By the Way

If you don't see Microsoft Excel 11.0 Object Library in your list of available COM references, you probably don't have Excel 2003 installed, and this example will not work.


By the Way

Visual C# doesn't work directly with COM components (as did previous versions of Visual C#). Instead, it interacts through a wrapper, a set of code and objects that works as an intermediary between Visual C# and a COM component. When you add the reference to a COM component, .NET automatically creates this wrapper for you.





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net