How to convert labels to map annotation


This sample converts labels to Map Annotation in an Annotation Group. The sample works similarly to ArcMap's Convert Labels To Annotation command (Feature layer context menu), but without the dialog or user interaction. The macro creates a new Annotation Group for the current data frame and populates it with labels from the first layer in the map. This sample works with maps being labeled with the ESRI Standard Label Engine or the ESRI Maplex Label Engine.

How to use

  1. Add a feature class to ArcMap. Set labeling properties on the layer.
  2. Copy/paste the macro code into VBA.
  3. Run the macro.
[VBA]
Option Explicit

Public Sub ConvertLabelsToMapAnno()
    
    Dim pMxDoc As IMxDocument
    Dim pActiveView As IActiveView
    Dim pMap As IMap
    Dim pConvertLabelsToAnnotation As IConvertLabelsToAnnotation
    Dim pEnumLayer As IEnumLayer
    Dim pFClass As IFeatureClass
    Dim pFeatureLayer As IFeatureLayer
    Dim pDataset As IDataset
    Dim pLayer As ILayer
    Dim pGeoFeatureLayer As IGeoFeatureLayer
    Dim pTC As ITrackCancel
    Dim pApp As IApplication
    
    Set pMxDoc = ThisDocument
    Set pActiveView = pMxDoc.ActiveView
    Set pMap = pActiveView.FocusMap 'get the focus map from the view
    Set pMapLayers = pMap
    Set pTC = New CancelTracker
    Set pConvertLabelsToAnnotation = New ConvertLabelsToAnnotation
    
    'change global level options for the conversion by sending in different parameters to the next line
    pConvertLabelsToAnnotation.Initialize pMap, esriMapAnnotation, esriVisibleFeatures, True, pTC
    
    'We'll convert the first layer in the map.  Change this index value to change which layer is converted
    'Labeling should be enable for this layer.
    Set pLayer = pMap.Layer(0)
    
    If Not TypeOf pLayer Is IFeatureLayer Then
        MsgBox "layer " & pLayer.Name & " is not a feature layer"
    End If
    
    Set pFeatureLayer = pLayer
    
    'add the layer information to the converter object.
    'Since this is just map annotation, specify only the layer and the annotation group name
    pConvertLabelsToAnnotation.AddFeatureLayer pFeatureLayer, pFeatureLayer.Name & "_Anno"
    
    'After setting everything up, now perform the conversion
    pConvertLabelsToAnnotation.ConvertLabels
    
    'Turn the labels for the converted layer off
    Set pGeoFeatureLayer = pLayer
    pGeoFeatureLayer.DisplayAnnotation = False
    pActiveView.ContentsChanged
    
    'Finally get a reference to the application and show the overflow dialog
    Set pApp = Application
    pApp.ShowDialog esriMxDlgOverflowLabels, True
    
End Sub