Visual Studio Tools for Office Project Events

Each Visual Studio Tools for Office project template automatically generates several event handlers. The event handlers for document-level customizations are slightly different from event handlers for application-level add-ins.

For more information about customizations and add-ins, see Office Solutions Development Overview.

Document-Level Customizations

Visual Studio Tools for Office 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 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 Tools for Office 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_Startup

    • Sheet2_Startup

    • Sheet3_Startup

    • ThisWorkbook_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 Tools for Office 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_Shutdown

    • Sheet2_Shutdown

    • Sheet3_Shutdown

    • ThisWorkbook_Shutdown

Note

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

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.

Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Startup

End Sub 

Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Shutdown

End Sub
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
}

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

The following code example shows the default event handlers in document-level projects for Excel.

Note

The following code example shows the event handlers in the Sheet1 class. The names of the event handlers in other host item classes correspond to the class name. For example, in the Sheet2 class, the Startup event handler is named Sheet2_Startup. In the ThisWorkbook class, the Startup event handler is named ThisWorkbook_Startup.

Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Startup

End Sub 

Private Sub Sheet1_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Shutdown

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

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

Application-Level Add-Ins

Visual Studio Tools for Office provides generated code in application-level add-ins. This code raises two different events: Startup and Shutdown.

Startup Event

The Startup event is raised after the 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 add-in overrides the RequestComAddInAutomationService method. In this case, the ThisAddIn_Startup event handler is called after RequestComAddInAutomationService. In add-ins for the 2007 Microsoft Office system, code in the ThisAddIn_Startup event handler might also be called after the RequestService method, if your add-in overrides this method.

For more information about the startup sequence of add-ins, see Architecture of Application-Level 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 add-in is unloaded.

See Also

Tasks

How to: Create Visual Studio Tools for Office Projects

Concepts

Developing Office Solutions

Programming Document-Level Customizations

Programming Application-Level Add-Ins

Visual Studio Tools for Office Project Templates Overview