English

How To: Add a date/time stamp to an ArcMap layout using ArcObjects

Summary

Instructions provided demonstrate how to add and update a date/time stamp to an ArcMap layout using ArcObjects and VBA code. The code runs on the OpenDocument event. Therefore the date/time stamp is modified every time the MXD is opened.

Before Beginning

To use this sample perform these preliminary steps:

A. Add a text element to the layout. The text element is populated with the date/time stamp.
B. Right-click on the text element and make a note of the xy location.
C. Use the xy location in the steps five and eight, below.

Procedure

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

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

  3. In the VBA project explorer window, expand Project.mxt to expose ThisDocument and open its code window.
  4. Select MxDocument from the Object dropdown list, and OpenDocument from the Procedure dropdown list in the code window.
    [O-Image] Visual Basic Editor
  5. Add this code to the OpenDocument procedure. Note - add the x/y location of the text element to the code below:

    Code:
    Dim pMxDoc As IMxDocument
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pPageLayout As IPageLayout
    Dim pEnumElement As IEnumElement
    Dim pTextElement As ITextElement
    Dim pElement As IElement

    Set pMxDoc = ThisDocument
    Set pPageLayout = pMxDoc.PageLayout
    Set pGraphicsContainer = pPageLayout

    Dim pPoint As IPoint
    Set pPoint = New Point

    'After adding the text element, remember the page coordinates.
    'enter the x and y values of your text element here
    pPoint.X = 7.5
    pPoint.Y = 0.6
    '

    Set pEnumElement = pGraphicsContainer.LocateElements(pPoint, 0.5)

    pEnumElement.Reset
    Set pElement = pEnumElement.Next
    If TypeOf pElement Is ITextElement Then
    pGraphicsContainer.DeleteElement pElement
    Call Project.AddDate.AddText
    Exit Function
    End If

  6. In the VBA project explorer window, add a new module to the project.
    [O-Image] Add new module to project
  7. Open the code window for the new module.
  8. Add this code to the module's code window. Note - add the x/y location of the text element to the code below:

    Code:
    Dim m_pMxDoc As IMxDocument
    Dim m_pPageLayout As PageLayout
    Dim m_pGContainer As IGraphicsContainer

    Public Sub AddElement(AnElement As IElement, PagePosition As IGeometry)
    Set m_pMxDoc = ThisDocument
    Set m_pPageLayout = m_pMxDoc.PageLayout
    AnElement.Geometry = PagePosition
    Set m_pGContainer = m_pPageLayout
    m_pGContainer.AddElement AnElement, 0
    m_pMxDoc.ActiveView.Refresh

    End Sub

    Public Sub AddText()
    Dim pTextElement As ITextElement
    Set pTextElement = New TextElement

    ''retrieve the document title
    Dim pDocumentTitle As String
    Dim pDocument As IDocument
    Set pDocument = Application.Document
    pDocumentTitle = pDocument.Title

    ''display
    pTextElement.Text = "The date / time is : " & Now & vbCrLf & _
    "The document name is : " & pDocumentTitle

    ''create a point for the location of the text
    Dim pPoint As IPoint
    Set pPoint = New Point

    'This is where the date stamp will be placed on the page
    'enter the x and y values of your text element here
    pPoint.X = 7.5
    pPoint.Y = 0.6
    '

    AddElement pTextElement, pPoint

    End Sub

  9. Change the name of the newly added module to AddDate.
    [O-Image] Change name of module to AddDate
  10. Save the MXD and close.
  11. Reopen the MXD. The layout is stamped with the date/time.