Add 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.

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.
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