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 application-level add-ins.

Applies to: The information in this topic applies to document-level projects and application-level projects for Microsoft Office 2013 and Microsoft Office 2010. For more information, see Features Available by Office Application and Project Type.

Document-Level Projects

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_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 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)
{
}

Order of Events in Document-Level Excel Projects

The Startup event handlers in Excel projects are called in this order:

  1. ThisWorkbook_Startup.

  2. Sheet1_Startup.

  3. Sheet2_Startup.

  4. Sheet3_Startup.

  5. Other sheets in order.

The Shutdown event handlers in a workbook solution are called in this order:

  1. ThisWorkbook_Shutdown.

  2. Sheet1_Shutdown.

  3. Sheet2_Shutdown.

  4. Sheet3_Shutdown.

  5. 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.

Application-Level Projects

Visual Studio 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.

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 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.

Shutdown Event in Outlook Add-ins

The Shutdown event is raised only when the user disables the 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:

Note

You can force Outlook to raise the Shutdown event when it exits by modifying the registry. However, if an administrator reverts this setting, any code that you add to the ThisAddIn_Shutdown method no longer runs when Outlook exits. For more information, see Shutdown Changes for Outlook 2010.

See Also

Tasks

How to: Create Office Projects in Visual Studio

Other Resources

Developing Office Solutions

Programming Document-Level Customizations

Programming Application-Level Add-Ins

Office Project Templates Overview