English

How To: Loop through the tables in ArcMap using VBA and ArcObjects

Summary

This article shows how to loop through all the tables in the first data frame in ArcMap. The table names will be displayed in a series of message boxes.

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:
    Sub GetTables()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    'Get the tables in the first data frame
    Dim pTc As ITableCollection
    Set pTc = pDoc.FocusMap
    Dim pTable As ITable

    'Loop through the tables
    Dim pDs As IDataset
    For i = 0 To pTc.TableCount - 1
    Set pTable = pTc.Table(i)
    'Get the table's name
    Set pDs = pTable
    MsgBox pDs.Name
    Next i
    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

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