Events in Office Projects
Each Office project template automatically generates several event handlers. The event handlers for document-level customizations are slightly different from event handlers for VSTO Add-ins.
Applies to: The information in this topic applies to document-level projects and VSTO add-in projects. See Features Available by Office Application and Project Type.
Visual Studio provides generated code behind new or existing documents or worksheets in document-level customizations. This code raises two different events: Startup and Shutdown.
Startup Event
The Startup event is raised for each of the host items (document, workbook or worksheet) after the document is running and all the initialization code in the assembly has been run. It is the last thing to run in the constructor of the class that your code is running in. For more information about host items, see Host Items and Host Controls Overview.
When you create a document-level project, Visual Studio creates event handlers for the Startup event in the generated code files:
For Microsoft Office Word projects, the event handler is named
ThisDocument_Startup.For Microsoft Office Excel projects, the event handlers have the following names:
Sheet1_StartupSheet2_StartupSheet3_StartupThisWorkbook_Startup
Shutdown Event
The Shutdown event is raised for each of the host items (document or worksheet) when the application domain that your code is loaded in is about to unload. It is the last thing to be called in the class as it unloads.
When you create a document-level project, Visual Studio creates event handlers for the Shutdown event in the generated code files:
For Microsoft Office Word projects, the event handler is named
ThisDocument_Shutdown.For Microsoft Office Excel projects, the event handlers have the following names:
Sheet1_ShutdownSheet2_ShutdownSheet3_ShutdownThisWorkbook_Shutdown
|
Event Handler Method Declarations
Every event handler method declaration has the same arguments passed to it: sender and e. In Excel, the sender argument refers to the sheet, such as Sheet1 or Sheet2; in Word, the sender argument refers to the document. The e argument refers to the standard arguments for an event, which are not used in this case.
The following code example shows the default event handlers in document-level projects for Word.
The following code example shows the default event handlers in document-level projects for Excel.
|
Order of Events in Document-Level Excel Projects
The Startup event handlers in Excel projects are called in this order:
ThisWorkbook_Startup.Sheet1_Startup.Sheet2_Startup.Sheet3_Startup.Other sheets in order.
The Shutdown event handlers in a workbook solution are called in this order:
ThisWorkbook_Shutdown.Sheet1_Shutdown.Sheet2_Shutdown.Sheet3_Shutdown.Other sheets in order.
The order is determined when the project is compiled. If the user rearranges the sheets at run time, it does not change the order that the events are raised the next time the workbook is opened or closed.
Visual Studio provides generated code in VSTO Add-ins. This code raises two different events: Startup and Shutdown.
Startup Event
The Startup event is raised after the VSTO Add-in is loaded and all the initialization code in the assembly has been run. This event is handled by the ThisAddIn_Startup method in the generated code file.
Code in the ThisAddIn_Startup event handler is the first user code to run, unless your VSTO Add-in overrides the RequestComAddInAutomationService method. In this case, the ThisAddIn_Startup event handler is called after RequestComAddInAutomationService.
Don’t add code in the ThisAdd-In_Startup event handler if the code requires a document to be open. Instead, add that code to an event that the Office application raises when a user creates or opens a document. For more information, see Accessing a Document When the Office Application Starts.
For more information about the startup sequence of VSTO Add-ins, see Architecture of VSTO Add-ins.
Shutdown Event
The Shutdown event is raised when the application domain that your code is loaded in is about to be unloaded. This event is handled by the ThisAddIn_Shutdown method in the generated code file. This event handler is the last user code to run when the VSTO Add-in is unloaded.
Shutdown Event in Outlook VSTO Add-ins
The Shutdown event is raised only when the user disables the VSTO Add-in by using the COM Add-ins dialog box in Outlook. It is not raised when Outlook exits. If you have code that must run when Outlook exits, handle either of the following events:
The E:Microsoft.Office.Interop.Outlook.ApplicationEvents_11_Event.Quit event of the T:Microsoft.Office.Interop.Outlook.Application object.
The E:Microsoft.Office.Interop.Outlook.ExplorerEvents_10_Event.Close event of the T:Microsoft.Office.Interop.Outlook.Explorer object.
|
Developing Office Solutions
How to: Create Office Projects in Visual Studio
Programming Document-Level Customizations
Programming VSTO Add-Ins
Office Project Templates Overview