How to add a text element to the layout


This sample code adds a text string to the page layout as a text element. A point object is used to specify where on the layout the text should appear.
Text elements can also be added to the map. - see the How to add a text element to the map sample for an example of this. Text added to the map is added in geographic space. This is useful, for example, if you want to annotate a particular feature. Conversely, when you add text to the page layout, you fix the location of the text in page space. This might be useful, fo example, to place a piece of text representing the map title or subject.

How to use

  1. Add a new UIToolControl to any toolbar.
  2. Paste the code into the UIToolControl's mouse down event.
  3. Mind the name of the control, the sample assumes it is UIToolControl1.
  4. Modify the text element string as desired.
  5. Completely shut down VBA so mouse events fire.
  6. Make sure ArcMap is in layout view.
  7. Select the tool and click somewhere on the layout to add the text element.
[VBA]
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pTextElement As ITextElement
    Dim pElement As IElement
    
    Set pMxDoc = Application.Document
    'Check ArcMap is in layout view
    If Not pMxDoc.ActiveView Is pMxDoc.PageLayout Then Exit Sub
    Set pActiveView = pMxDoc.PageLayout
    Set pGraphicsContainer = pMxDoc.PageLayout
    
    Set pTextElement = New TextElement
    Set pElement = pTextElement
    
    pTextElement.Text = "My Map"
    pElement.Geometry = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)
    pGraphicsContainer.AddElement pTextElement, 0
    
    pMxDoc.ActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
End Sub