This step-by-step article describes how to handle Microsoft Excel events from an Automation client that is developed with Visual C# .NET.
Overview of Event Handling
Visual C# .NET uses delegates to handle events from a Component Object Model (COM) server. Delegates are a new concept in Microsoft Visual Studio .NET. With COM events, a delegate is a special object that listens for events from the COM server and then forwards them to a Visual C# function.
To use a delegate, you must create an instance of the object, and then add the instance of the object to the event that you want to listen to. Each event has a delegate that is specifically designed to translate the COM event (with native data types) to a standard Microsoft .NET call (with managed data types).
Create the Visual C# .NET Automation Client
To use delegates to handle Excel events from an Automation client that is developed with Visual C# .NET, follow these steps:
Start Visual Studio .NET 2002 or Visual Studio .NET 2003. On the
and then click
Visual C# Projects, select
Windows Application. Name the project XLEventTest, and then click OK.
By default, Form1 is created.
Add a reference to the
Microsoft Excel Object Library. To do this, follow these steps:
Microsoft Excel 11.0 Object Library,
and then click
dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click
In Solution Explorer, double-click
to display the form in Design view.
to display the Toolbox, and then add one button to Form1. Change the
property of the button to
- Double-click Start Excel to display the Code window for the form. Add the following code to the
event handler for the button:
private void button1_Click(object sender, System.EventArgs e)
- Add the following code near the top of the file, but below the other
using Excel = Microsoft.Office.Interop.Excel;
Add the following code to the Form1 class below the
event handler from step 5:
//Excel Automation variables:
Excel.Worksheet xlSheet1, xlSheet2, xlSheet3;
//Excel event delegate variables:
private void StartExcelAndSinkEvents()
//Start Excel, and then create a new workbook.
xlApp = new Excel.Application();
xlBook = xlApp.Workbooks.Add( Missing.Value );
xlBook.Windows.get_Item(1).Caption = "XL Event Test";
xlSheet1 = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlSheet2 = (Excel.Worksheet)xlBook.Worksheets.get_Item(2);
xlSheet3 = (Excel.Worksheet)xlBook.Worksheets.get_Item(3);
//Add an event handler for the WorkbookBeforeClose Event of the
new Excel.AppEvents_WorkbookBeforeCloseEventHandler( BeforeBookClose);
xlApp.WorkbookBeforeClose += EventDel_BeforeBookClose;
//Add an event handler for the Change event of both worksheet objects.
EventDel_CellsChange = new Excel.DocEvents_ChangeEventHandler( CellsChange);
xlSheet1.Change += EventDel_CellsChange;
xlSheet2.Change += EventDel_CellsChange;
xlSheet3.Change += EventDel_CellsChange;
//Make Excel visible and give the user control.
xlApp.Visible = true;
xlApp.UserControl = true;
private void CellsChange(Excel.Range Target )
//This is called when any cell on a worksheet is changed.
Debug.WriteLine("Delegate: You Changed Cells " +
Target.get_Address( Missing.Value, Missing.Value,
Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value ) +
" on " + Target.Worksheet.Name);
private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel )
//This is called when you choose to close the workbook in Excel.
//The event handlers are removed, and then the workbook is closed
//without saving the changes.
Wb.Saved = true;
Debug.WriteLine("Delegate: Closing the workbook and removing event handlers.");
xlSheet1.Change -= EventDel_CellsChange;
xlSheet2.Change -= EventDel_CellsChange;
xlSheet3.Change -= EventDel_CellsChange;
xlApp.WorkbookBeforeClose -= EventDel_BeforeBookClose;
Test the Code
- Press CTRL+ALT+O to display the Output window.
- Press F5 to build and then run the program.
On the form, click
the Start Excel
The program starts Excel and then creates a workbook with three worksheets.
- Add any data to cells on any of the worksheets.
Look at the Output window in Visual Studio to verify that the event handlers are called.
- Quit Excel, and then close the form to end the debug session.
When you compile the code, you may receive the following compiler error message:
Namespace '' already contains a definition for 'Excel'
You receive this error message if you do not have the Primary Interop Assembly (PIA) for Excel installed. To resolve this problem, follow these steps:
- Run Microsoft Office setup, and then install the Excel PIA. In Office setup, the PIA appears as a component under Excel as .NET Programmability Support.
- Open your project, remove the reference to the Excel interop assembly, and then repeat step 2 in the "Create the Visual C# .NET Automation Client" section of this article to correctly reference the PIA.
When you test the code, you may receive the following error message:
An unhandled exception of type 'System.InvalidCastException' occurred in interop.excel.dll
Additional information: No such interface supported
For additional information about this error message, click the following article number to view the article in the Microsoft Knowledge Base:
PRB: Error Using WithEvents or Delegates to Handle Excel Events from Visual Basic .NET or Visual C# .NET
For additional information, visit the following Microsoft Developer Network (MSDN) Web site:
For additional information about automating Excel from Visual C# .NET , click the following article numbers to view the articles in the Microsoft Knowledge Base:
HOWTO: Automate Microsoft Excel from Microsoft Visual C# .NET
HOWTO: Automate Excel With Visual C# .NET To Fill or Obtain Data In a Range Using Arrays
HOWTO: Binding for Office Automation Servers with Visual C#
Article ID: 823981 - Last Review: January 17, 2007 - Revision: 2.4
- Microsoft Office Excel 2003
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition