English

How To: Programmatically change the template when opening an MXD

Summary

Instructions below provide an example of how to apply template changes to a map document as the document opens in ArcMap. Note that this code will not work with ArcGIS 9.x. See the Related Information section at the bottom of this document for an alternative method of programmtically changing templates.

Procedure

This code is stored in a map template and executes when a MXD referencing the MXT is opened.

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer

    Note:
    Code in the Project's ThisDocument code module will run when a map document referencing the MXT is opened.

  4. Copy the following code into the code module.

    Code:
    Private Function MxDocument_OpenDocument() As Boolean

    Dim pDoc As IMxDocument
    Dim pApp As IApplication
    Dim sTemplate As String

    ' When the MXD is opened, the HEATHER.MXT
    ' template will be opened.
    Set pDoc = ThisDocument
    Set pApp = Application
    sTemplate = "c:\temp\heather.mxt"

    Dim pChangeLayout As IChangeLayout
    Set pChangeLayout = pDoc

    SendKeys "{TAB}{TAB}{ENTER}" & sTemplate & "{ENTER}{ENTER}", False

    pChangeLayout.ChangeLayout

    End Function

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Related Information