Microsoft KB Archive/320523

= HOW TO: Check the Type of a COM Object (System.__ComObject) with Visual C# .NET =

Article ID: 320523

Article Last Modified on 12/15/2003

-

APPLIES TO


 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q320523



IN THIS TASK

 * SUMMARY
 * Step-by-Step
 * Try It Out



SUMMARY
In the Microsoft .NET Framework, the GetType.Type operator for a COM object will return the System.__ComObject class. In some coding scenarios, you may have to know the specific class for an object. For example, an Automation client for Microsoft Excel may have to determine the type of object that the user has selected in Excel.

In Microsoft Visual C# .NET, the as operator is similar to a cast operator, except that it yields null on conversion failure instead of raising an exception. You can use the as operator to compare a COM object with a specific type. If the cast does not return null, the conversion is successful.

The following step-by-step procedure demonstrates how you can use the as operator to check the type of a COM object by using Visual C# .NET.

Step-by-Step
 Create a new Windows Application project in Visual C# .NET. By default, Form1 is created. Add a reference to Microsoft Excel Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. In the Add Reference dialog box, click the COM tab, click the Microsoft Excel Object Library, and then click Select.

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

328912 INFO: Microsoft Office XP PIAs Are Available for Download

 To accept your selection, click OK.  On the View menu, select Toolbox, and then add a button and a text box to Form1.</li> To display the code window for the form, double-click Form1.</li>  Replace the Form1_Load handler private void Form1_Load(object sender, System.EventArgs e) { } with the following: Excel.ApplicationClass oExcel; Excel.Workbooks oBooks; Excel.Workbook oBook;

private void Form1_Load(object sender, System.EventArgs e) { //Start Microsoft Excel with a new workbook and give control //to the user. oExcel = new Excel.ApplicationClass; oBooks = oExcel.Workbooks; oBook = oBooks.Add(System.Reflection.Missing.Value); oExcel.Visible=true; oExcel.UserControl=true;

//Layout the controls on Form1 and set up the Click event //handler for the button. textBox1.Text =&quot;&quot;; textBox1.Width = 200;

button1.Text = &quot;Get Selection&quot;; this.button1.Click += new System.EventHandler(this.ButtonClick); }

private void ButtonClick(object sender, System.EventArgs e) { object o = oExcel.Selection;

//Display a message about the selection type in Excel. if((o as Excel.Range)!=null) {       textBox1.Text = &quot;Selection is Excel.Range&quot;; }   else if((o as Excel.Rectangle)!=null) {       textBox1.Text = &quot;Selection is Excel.Rectangle&quot;; }   else if((o as Excel.ChartObject)!=null) {       textBox1.Text = &quot;Selection is Excel.ChartObject&quot;; }   else if((o as Excel.ChartArea)!=null) {       textBox1.Text = &quot;Selection is Excel.ChartArea&quot;; }   // ... There are many additional Selection types you could check for if needed ... else {       textBox1.Text = &quot;Selection is Unknown&quot;; }

}                   </li>  Add the following USING statement to the code module: using Excel = Microsoft.Office.Interop.Excel; </li></ol>

back to the top

Try It Out

 * 1) Press F5 to build and to run the program. When the form loads, Microsoft Excel starts with a new workbook.
 * 2) Arrange Form1 and the Excel application windows so that you can view both.
 * 3) In Excel, click any cell in the workbook.
 * 4) Click Get Selection on Form1. The text box displays &quot;Selection is Excel.Range&quot;.
 * 5) On the Insert menu in Excel, click Chart, and then click Finish in the chart wizard.
 * 6) Click Get Selection on Form1. The text box displays &quot;Selection is Excel.ChartArea&quot;.
 * 7) In Excel, draw a rectangle on the workbook, and then select the rectangle.

Note You can use the Rectangle control on the Drawing toolbar to draw a rectangle on the worksheet.
 * 1) Click Get Selection on Form1. The text box displays &quot;Selection is Excel.Rectangle&quot;.

back to the top

Additional query words: typename xl2003

Keywords: kbhowtomaster kbautomation kbpia KB320523

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.