HOW TO
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 BeginningTo 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.
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
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
Article ID:000004890
Get help from ArcGIS experts
Download the Esri Support App