HOW TO: Check the Type of a COM Object (System.__ComObject) with Visual C# .NET (320523)
The information in this article applies to:
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
- Microsoft Office Excel 2003
- Microsoft Excel 2002
This article was previously published under Q320523 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.
- To display the code window for the form, double-click Form1.
- 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";
}
}
- Add the following USING statement to the code module:
using Excel = Microsoft.Office.Interop.Excel;
back to the top
Try It Out- Press F5 to build and to run the program. When the form loads, Microsoft Excel starts with a new workbook.
- Arrange Form1 and the Excel application windows so that you can view both.
- In Excel, click any cell in the workbook.
- Click Get Selection on Form1. The text box displays "Selection is Excel.Range".
- On the Insert menu in Excel, click Chart, and then click Finish in the chart wizard.
- Click Get Selection on Form1. The text box displays "Selection is Excel.ChartArea".
- 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. - Click Get Selection on Form1. The text box displays "Selection is Excel.Rectangle".
back to the top
Modification Type: | Major | Last Reviewed: | 12/15/2003 |
---|
Keywords: | kbPIA kbAutomation kbHOWTOmaster KB320523 kbAudDeveloper |
---|
|