English

How To: Update ArcMap TOC symbology headings from a table

Summary

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

Procedure

  1. Get a reference to the map document whose headings are to 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. Create a pointer to an INFO file workspace.

    '++ Create a pointer to the data's workspace:
    Dim pWorkspaceFact As IWorkspaceFactory
    Set pWorkspaceFact = New ArcInfoWorkspaceFactory
    Dim pWorkspace As IWorkspace
    Set pWorkspace = pWorkspaceFact.OpenFromFile("c:\geodata\covers", 0)
    Dim pFeatureWorkspace As IFeatureWorkspace
    Set pFeatureWorkspace = pWorkspace

  4. Use IFeatureWorkspace:OpenTable to open the INFO file as an ITable object; then, select all table rows using a Query Filter.

    Dim pInfoTable As ITable
    Set pInfoTable = pFeatureWorkspace.OpenTable("headings.lut")
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    pQueryFilter.WhereClause = "FIELD_OF_HEADING_NAMES <> ''"
    Dim pCursor As ICursor
    Set pCursor = pInfoTable.Search(pQueryFilter, False)
    Dim fieldindex As Long
    fieldindex = pCursor.FindField("field_of_heading_names")
    Dim pRow As IRow
    Set pRow = pCursor.NextRow

  5. Loop through the TOC layers. You must loop through each layer because each layer can have multiple subheadings. Change each heading to the value from the heading field in the INFO table. Advance the cursor to the next table row and 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)
    strMyHeading = pRow.Value(fieldindex)
    pLegendGroup.Heading = strMyHeading
    Set pRow = pCursor.NextRow
    Next j
    Set pFeatureLayer = pLayers.Next
    Next i
    pMxDocument.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