HOW TO

Use IAnnotateLayerPropertiesCollection::QueryItem to return the placed label elements

Last Published: April 25, 2020

Summary

This article contains VBA sample code that returns the text and position for all labels in a layer.

Procedure



  1. Open 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 ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.

  4. Paste the following code into the code module.

    Code:
    Public Sub GetLabels()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap

    'Get the first layer in ArcMap
    Dim pGLayer As IGeoFeatureLayer
    Set pGLayer = pMap.Layer(0)
    Dim pAnnoLayerPropsCol As IAnnotateLayerPropertiesCollection
    Dim pAnnoLayerProps As IAnnotateLayerProperties
    Dim pAnnoProps As IAnnotateProperties

    Dim pEnumVisibleElements As IElementCollection
    Set pEnumVisibleElements = New ElementCollection

    ' Get the Annotation Collection
    Set pAnnoLayerPropsCol = pGLayer.AnnotationProperties

    pGLayer.DisplayAnnotation = True

    pAnnoLayerPropsCol.QueryItem 0, pAnnoLayerProps, _
    pEnumVisibleElements, Nothing
    pAnnoLayerProps.LabelWhichFeatures = esriVisibleFeatures
    pAnnoLayerProps.UseOutput = True

    'Populate visible element collection
    Dim pAnnotateMap As IAnnotateMap
    Set pAnnotateMap = pMap.AnnotationEngine
    Dim pAnnotateMapProps As IAnnotateMapProperties
    Set pAnnotateMapProps = New AnnotateMapProperties
    Set pAnnotateMapProps.AnnotateLayerPropertiesCollection = _
    pAnnoLayerPropsCol
    Dim pTC As ITrackCancel
    Set pTC = New CancelTracker
    Set pAnnoLayerProps.FeatureLayer = pGLayer
    pAnnotateMap.Label pAnnotateMapProps, pMap, pTC
    Set pAnnoLayerProps.FeatureLayer = Nothing

    ' Query the element collection for the visible labels
    Dim i As Integer
    Dim pElement As IElement
    Dim pTextElement As ITextElement
    Dim sText As String
    Dim pPolyline As IPolyline

    For i = 0 To pEnumVisibleElements.Count - 1
    pEnumVisibleElements.QueryItem i, pElement
    Set pTextElement = pElement
    If (Not pTextElement Is Nothing) Then sText = pTextElement.Text
    Set pPolyline = pElement.Geometry
    MsgBox sText & " " & pPolyline.FromPoint.X & _
    " " & pPolyline.FromPoint.Y
    Next i
    End Sub

  5. Close the Visual Basic Editor.
  6. Add a layer to ArcMap.
  7. Right-click the layer and select Label Features.
  8. Run the code.

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

Article ID:000004960

Software:
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic