English

How To: Update ArcMap TOC symbology headings from a text file

Summary

Steps to develop VBA code to update Table Of Contents (TOC) symbology headings from a text file prior to inserting a legend in a map document.

Procedure

  1. Get a reference to the map document whose headings will be changed.

    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDocument.FocusMap

  2. Get a list of all feature layers in the TOC.

    Dim pLayers As IEnumLayer
    Set pLayers = pMap.Layers
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pLayers.Next

  3. Open the text file containing the desired headings. The heading strings in the file must be enclosed in quotes ("My Heading").

    Code:
    Open "c:\geodata\files\headings.txt" For Input Access Read As #1

  4. Loop through the TOC layers. Since there may be multiple subheadings for the symbolization of a given layer, you must create a loop for each layer. Change each heading to the text string read from the current line of the text file with a VBA INPUT statement; then, advance the loop to the next TOC heading.

    Code:
    Dim i As Integer
    Dim j As Integer
    Dim pLegendInfo As ILegendInfo
    Dim pLegendGroup As ILegendGroup
    Dim strMyHeading As String
    For i = 0 To pMap.LayerCount - 1
    Set pLegendInfo = pFeatureLayer
    For j = 0 To pLegendInfo.LegendGroupCount - 1
    Set pLegendGroup = pLegendInfo.LegendGroup(j)
    Input #1, strMyHeading
    pLegGroup.Heading = strMyHeading
    Next j
    Set pFeatureLayer = pLayers.Next
    Next i
    Close #1
    pMxDoc.UpdateContents


    Warning:
    Run this code before you insert a legend from the Insert menu. Since this code does not capture legend properties, running it after inserting the legend would not update the legend graphic and you'd have to be rebuild from scratch.


    Note:
    This is not ESRI supported code. Use it at your own risk. Make a backup of your data before running this code and carefully check the output. This is just example code with no real error checking. It is meant to be a guide to develop code specific to your data inputs and desired outputs.

Related Information