Condividi tramite


Events in Office projects

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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.

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 void Sheet1_Startup(object sender, System.EventArgs e)
{
}

private void Sheet1_Shutdown(object sender, System.EventArgs e)
{
}
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

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:

  6. ThisWorkbook_Shutdown.

  7. Sheet1_Shutdown.

  8. Sheet2_Shutdown.

  9. Sheet3_Shutdown.

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

VSTO Add-in projects

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 Access 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:

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