Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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

  1. Create a new Windows Application project in Visual C# .NET. By default, Form1 is created.
  2. Add a reference to Microsoft Excel Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. 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
    3. To accept your selection, click OK.
  3. On the View menu, select Toolbox, and then add a button and a text box to Form1.
  4. To display the code window for the form, double-click Form1.
  5. 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 ="";
    	textBox1.Width = 200;
    
    
    	button1.Text = "Get Selection";
    	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 = "Selection is Excel.Range";
    	}
    	else if((o as Excel.Rectangle)!=null)
    	{
    		textBox1.Text = "Selection is Excel.Rectangle";
    	}
    	else if((o as Excel.ChartObject)!=null)
    	{
    		textBox1.Text = "Selection is Excel.ChartObject";
    	}
    	else if((o as Excel.ChartArea)!=null)
    	{
    		textBox1.Text = "Selection is Excel.ChartArea";
    	}
    	// ... There are many additional Selection types you could check for if needed ...
    	else
    	{
    		textBox1.Text = "Selection is Unknown";
    	}
    
    }
    					
  6. Add the following USING statement to the code module:
    using Excel = Microsoft.Office.Interop.Excel;

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 "Selection is Excel.Range".
  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 "Selection is Excel.ChartArea".
  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.
  8. Click Get Selection on Form1. The text box displays "Selection is Excel.Rectangle".

↑ Back to the top


Keywords: KB320523, kbhowtomaster, kbautomation, kbpia

↑ Back to the top

Article Info
Article ID : 320523
Revision : 8
Created on : 12/15/2003
Published on : 12/15/2003
Exists online : False
Views : 565