

WorkbookBeforeClose Event: This event gets fired before workbook close action. Private void Application_WorkbookDeactivate(Excel.Workbook wb) private void ThisAddIn_Startup(object sender, System.EventArgs e)Īpplication.WorkbookDeactivate += Application_WorkbookDeactivate WorkbookDeactivate Event: This event gets fired when a workbook gets deactivated and wb object holds the reference for the same. Private void Application_WorkbookActivate(Excel.Workbook wb) private void ThisAddIn_Startup(object sender, System.EventArgs e)Īpplication.WorkbookActivate += Application_WorkbookActivate WorkbookActivate Event: This event gets fired when a workbook gets activated and wb object holds the reference for the same. Private void ThisAddIn_NewWorkbook(Excel.Workbook wb) ((Excel.AppEvents_Event)Application).NewWorkbook += ThisAddIn_NewWorkbook

NewWorkbook Event: This events trigger upon adding a new workbook private void ThisAddIn_Startup(object sender, System.EventArgs e) do whatever you want with wb object which refers to current workbook Private void OpenworkbookHandler(Excel.Workbook wb) += new Excel.AppEvents_WorkbookOpenEventHandler(OpenworkbookHandler)
EXCEL VBA ON OPEN SHEET EVENT CODE
Note: all application level events must be declared globally withing ThisAddin.cs to maintain their lifetime.Įxcel/Application Events: C# code example WorkbookOpen event private void ThisAddIn_Startup(object sender, System.EventArgs e) The Excel events can be classified in two categories: As long as a handler adheres to this signature, the event source can invoke it when required. The event source publishes a defined event signature which all event handlers must adhere to. The event handler (or subscriber as some people call it) is the custom code (i.e., our code) that is invoked by the event source when the event happens. In the case of Excel, the event source is an instance of the Excel class that defines the event, such as a Workbook instance or a Worksheet instance. There are two broad parts of event handling: the event source and the event handler. The sinking and dispatching just happens automatically for us. When we trap these events from VBA we’re not really aware of the underlying mechanisms that enable Excel to callback into our code. They are defined in COM interfaces exported from Excel.exe.
