Visual Studio Tools for Office Project Events
Each Visual Studio Tools for Office project template automatically generates several events. The events for document-level customizations are slightly different from events for application-level add-ins.
For more information about customizations and add-ins, see Office Solutions Development Overview.
Document-Level Customizations
Microsoft Visual Studio 2005 Tools for the Microsoft Office System (VSTO 2005) provides generated code behind new or existing documents or worksheets in document-level customizations. This code raises two different events:
-
Startupevent: handled byThisDocument_Startup(Microsoft Office Word 2003) orSheet1_StartuporThisWorkbook_Startup(Microsoft Office Excel 2003). -
Shutdownevent: handled byThisDocument_Shutdown(Word) orSheet1_ShutdownorThisWorkbook_Shutdown(Excel).
Method declarations to handle these events are provided in the generated code behind the document.
Startup Event
Startup is raised 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.
Shutdown Event
Shutdown is raised for each of the host items (document or worksheets) 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. For more information about host items, see Host Items and Host Controls Overview.
Note |
|---|
|
Do not programmatically remove controls during the |
Event Handler Method Declarations
Each event handler method declaration has the same arguments passed into 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 contains the method declarations for the initialization event handlers in Word.
private void ThisDocument_Startup(object sender, System.EventArgs e) { } private void ThisDocument_Shutdown(object sender, System.EventArgs e) { }
The following code contains the method declarations for the event handlers in Excel.
Note |
|---|
|
The name of the method should correspond to the class name. For example, in the |
Application-Level Add-ins
VSTO 2005 and Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System (VSTO 2005 SE) provide generated code in application-level add-ins. This code raises two different events:
-
Startupevent: handled byThisAddIn_Startup(for add-ins created by using VSTO 2005 SE) orThisApplication_Startup(for add-ins created by using VSTO 2005). -
Shutdownevent: handled byThisAddIn_Shutdown(VSTO 2005 SE) orThisApplication_Shutdown(VSTO 2005).
Method declarations to handle these events are provided in the generated code file.
Startup Event
Startup is raised after the add-in is loaded and all the initialization code in the assembly has been run.
In Microsoft Office 2003 add-ins, code in the ThisApplication_Startup (if you are using VSTO 2005) or ThisAddIn_Startup (if you are using VSTO 2005 SE) event handlers is the first user code to run.
In add-ins created for the 2007 Microsoft Office system applications by using VSTO 2005 SE, code in the ThisAddIn_Startup event handler is the first user code to run, unless your add-in customizes the Ribbon user interface (UI) or creates an Outlook form region. In this case, your override of the RequestService method is the first user code to run.
For more information about customizing the Ribbon UI, see Ribbon Extensibility Overview. For more information about Outlook form regions, see Outlook Form Regions Overview.
Shutdown Event
Shutdown is raised when the application domain that your code is loaded in is about to be unloaded. It is the last user code to run when the add-in is unloaded.
