To handle the events you need to do two things:
1) Declare a handler method with the required signature
public void Application_SheetActivate(object sh)
2) Connect your handler method to the Excel object model that raises this event
Visual Studio displays a popup tooltip and will actually generate most of this line for you.
As soon as you type "+" a popup tooltip will be displayed
If you press Tab key twice Visual Studio generates the rest of the line and the event handler for you automatically
You can also use the Properties window to add event handlers
Double click the workbook class
Click on the lightning bolt icon to show events associated with this object
A lot of the events in the Excel object model are repeated on the Application, Workbook and Worksheet objects
For a complete list of all the events, please refer to Excel > Macros > Events
Events and Methods with the same name
There are several cases when an object has both an event and a method with the same name
To handle these events you must cast your object to the interface that contains the corresponding event definition
Here is an example of how to declare an event handler for the Workbook level NewSheet event
public void Event_WorkbookNewWorksheet(object sheet)
However there are a few events which have ambiguous names.
In these situations you must cast the object to the corresponding interface first.
Application object must be cast to AppEvents_Event.
Workbook object must be cast to WorkbookEvents_Event.
Worksheet object must be cast to DocEvents_Event.
Chart object must be cast to ChartEvents_Event.
You can avoid these ambiguous interfaces in VB.Net if you use WithEvents and Handles rather than using the AddHandler.
Application - NewWorkbook
NewWorkbook is the name of both a property and an event.
public void Event_ApplicationNewWorkbook(Excel.Workbook workbook)
Addhandler CType(gApplicationExcel, Excel.AppEvents_Event).NewWorkbook
© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext