Export (0) Print
Expand All

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:

  • Startup event: handled by ThisDocument_Startup (Microsoft Office Word 2003) or Sheet1_Startup or ThisWorkbook_Startup (Microsoft Office Excel 2003).

  • Shutdown event: handled by ThisDocument_Shutdown (Word) or Sheet1_Shutdown or ThisWorkbook_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.

NoteNote

Do not programmatically remove controls during the Shutdown event handler of the document. The UI elements of the document are no longer available when the Shutdown event occurs. If you want to remove controls before the application closes, add your code to another event handler such as BeforeClose or BeforeSave.

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.

NoteNote

The name of the method should correspond to the class name. For example, in the Sheet1 class, use method names such as Sheet1_Startup, and in the Sheet2 class, use method names such as Sheet2_Startup. In the ThisWorkbook class, use method names such as ThisWorkbook_Startup.

private void Sheet1_Startup(object sender, System.EventArgs e)
{
}

private void Sheet1_Shutdown(object sender, System.EventArgs e)
{
}

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:

  • Startup event: handled by ThisAddIn_Startup (for add-ins created by using VSTO 2005 SE) or ThisApplication_Startup (for add-ins created by using VSTO 2005).

  • Shutdown event: handled by ThisAddIn_Shutdown (VSTO 2005 SE) or ThisApplication_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.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft