This sample converts labels to feature-linked Geodatabase annotation. 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 feature class in the same workspace as the Geodatabase layer in the map (the annotation feature class name is hardcoded in the macro). Labeling properties are taken from the first layer in the map. The annotation feature class is populated with annotation elements from the first layer, labels are turned off for the original layer, and finally the map and TOC are refreshed. This sample works with maps being labeled with the ESRI Standard Label Engine and the ESRI Maplex Label Engine.
How to use
- Add a Geodatabase feature class to ArcMap. Set labeling properties on layer.
- Copy/paste the macro code into VBA.
- Run the macro.
Option Explicit
Public Sub ConvertLabelsToAnno()
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 pMapLayers As IMapLayers
Dim pTC As ITrackCancel
Dim pAnnoEnumLayers As IEnumLayer
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, esriDatabaseAnnotation, esriAllFeatures, 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
Set pFClass = pFeatureLayer.FeatureClass
Set pDataset = pFClass
'add the layer information to the converter object. Specifiy the parameters of the output annotation feature class here as well.
pConvertLabelsToAnnotation.AddFeatureLayer pFeatureLayer, pFeatureLayer.Name & "_Anno", pDataset.Workspace, pFClass.FeatureDataset, _
False, False, False, True, True
'After setting everything up, now perform the conversion
pConvertLabelsToAnnotation.ConvertLabels
Set pAnnoEnumLayers = pConvertLabelsToAnnotation.AnnoLayers
'Turn the labels for the converted label off
Set pGeoFeatureLayer = pLayer
pGeoFeatureLayer.DisplayAnnotation = False
'add the anno layer to the map
pMap.AddLayers pAnnoEnumLayers, True
End Sub