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 handlerwith the following:
private void Form1_Load(object sender, System.EventArgs e) { }
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;
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".