Working with Documents That Contain Startup Code

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Using automation to open a document does not prevent a document's startup code from running. Startup code can be defined in various ways in Microsoft® Office applications, as explained in the following table.

Application Startup code location
Word Startup code is contained in the event procedures for the Open or New events in the ThisDocument module of a document or template.
Excel Startup code is contained in the event procedure for the Open event in the ThisWorkbook module of a workbook or template.
Outlook Startup code is contained in the event procedure for the Startup event in the ThisOutlookSession of the local Outlook VBA project.
Access If you create an Access macro named AutoExec, this macro's actions will run on startup.

You can also place startup code in the event procedure for the startup form's Open event. To specify a form to be opened on startup, use the Startup command on the Tools menu.

Note   Microsoft® PowerPoint® and Microsoft® FrontPage® documents don't have a way to define startup code.

Because startup code might display message boxes or modal forms that act as dialog boxes, these message or dialog boxes might prevent your code from proceeding until a user closes or responds to them. If you have startup code in a Microsoft® Excel workbook or a Microsoft® Access database that you don't want to run if the document is opened programmatically from another application, you can use the UserControl property of the Application object to determine how a document is being opened and then act accordingly. If you can't use the UserControl property, you might need to use a SendKeys statement to send keystrokes to close the message or dialog box.

In Excel, the UserControl property will return False only when the document or workbook is opened from automation by using a hidden instance of the Excel Application object (Application.Visible = False). For example, the following code defined in an Excel workbook's Open event procedure will run only if the workbook is opened by a user or a visible instance of the Excel Application object. If you open the workbook by using a hidden instance of the Excel Application object from code running in another application, the message box won't be displayed.

Private Sub Workbook_Open()
   Dim strMsg As String
   
   strMsg = "This message was triggered by this workbook's " & _
          "Open event." & vbCrLf & _
          "It won't be displayed if this workbook " & _
          "is opened by using a hidden" & vbCrLf & _
          "instance of the Excel Application object " & _
          "from Automation code."
   
   ' If opened through Automation by using a hidden instance,
   ' the UserControl property will be False.
   If Application.UserControl = True Then
      MsgBox strMsg
   End If
End Sub

**Note   **In Microsoft® Word 97 and later, there is no way to prevent Open event code from running with the UserControl property. If Word is visible to the user, or if you call the UserControl property of a Word Application or Document object from within a Word code module, this property will always return True. However, you can still use the Word UserControl property from automation code (that creates a hidden instance of Word) running from another application to determine if a document was opened programmatically or by the user.

In Access, you don't have to check or keep track of whether the instance of the Application object is hidden or visible because the UserControl property is False whenever the application is started from code. To control whether code in the startup form's Open event is executed, Access provides a Cancel argument for the Open event. As shown in the following example, you can set the Cancel argument to True to keep a startup form from opening if you open the database by using automation code:

Private Sub Form_Open (Cancel As Integer)
   ' If database is opened from Automation, 
   ' cancel the Open event of the form.
   If Application.UserControl = False Then
      Cancel = True
   Else
   ' Any startup code that needs to run when the
   ' database is opened by a user goes here.
   End If
End Sub

You can also use the UserControl property of the Access Application object to control whether actions in a database's AutoExec macro will run when the database is opened from another program by using automation. To do this, you must enter Application.UserControl = True in the Condition column for each action you want to cancel. (To display the Condition column, click Conditions on the View menu.)

Tip   You can also use COM add-ins to implement a startup form or code. COM add-ins support events that you can use to determine how an application was loaded before connecting the add-in.

See Also

Office Application Automation | Setting References | Object Variable Declaration | Creation of Object Variables to Automate Another Office Application | Automating the Visual Basic Editor | The Set Statement and the New Keyword in Automation | Single-Use vs. Multi-Use Applications | Using the CreateObject and GetObject Functions | Shutting Down Objects Created by Using Automation | Add-ins, Templates, Wizards, and Libraries