Programatically export all layers in the ArcMap TOC

Last Published: April 25, 2020


Instructions provided demonstrate how to export all layers in the ArcMap TOC programatically.


  1. Start 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
    Code in the Project's ThisDocument code module will only run in the current map document. To store the code in all map documents open the Normal.mxt > ThisDocument code module instead.

  4. Copy the following code into the code module.

    Public Sub ExportAll_TOC()

    Dim pDoc As IMxDocument
    Dim pEnumLayer As IEnumLayer
    Dim pLayer As IFeatureLayer
    Dim pName As IName
    Dim pLayerSet As ISet
    Dim pMap As IMap
    Dim pFC As IFeatureClass
    Dim pINFeatureClassName As IFeatureClassName
    Dim pDataset As IDataset
    Dim pInDSName As IDatasetName
    Dim pFields As IFields
    Dim pField As IField
    Dim pGeometryDef As IGeometryDef
    Dim pFeatureClassName As IFeatureClassName
    Dim pOutDatasetName As IDatasetName
    Dim pWorkspaceName As IWorkspaceName
    Dim pExportOp As IExportOperation

    Set pDoc = Application.Document
    Set pMap = pDoc.FocusMap
    Set pEnumLayer = pMap.Layers
    Set pLayerSet = New esriSystem.Set
    Set pLayer = pEnumLayer.Next

    Do Until pLayer Is Nothing
    'Enumerate through the layers in the TOC and
    'Get the Feature Class Name from the featureclass
    Set pFC = pLayer.FeatureClass
    Set pDataset = pFC
    Set pINFeatureClassName = pDataset.FullName
    Set pInDSName = pINFeatureClassName
    Set pFields = pFC.Fields
    Set pField = pFields.Field(lGeomIndex)
    Set pGeometryDef = pField.GeometryDef
    Set pFeatureClassName = New FeatureClassName
    Set pOutDatasetName = pFeatureClassName

    'Assign name to each exported layer
    pLayerSet.Add pLayer
    For i = 0 To pLayerSet.Count - 1
    X = pLayerSet.Count - 1
    pOutDatasetName.Name = pDataset.Name & "_Export"
    Next i

    Set pWorkspaceName = New WorkspaceName
    pWorkspaceName.PathName = "C:\temp"
    pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory"
    Set pOutDatasetName.WorkspaceName = pWorkspaceName

    'Export the layers in the TOC
    Set pExportOp = New ExportOperation
    pExportOp.ExportFeatureClass pInDSName, Nothing, Nothing, pGeometryDef, _
    pOutDatasetName, hWnd

    Set pLayer = pEnumLayer.Next


    'Update the table of contents

    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.

Article ID:000007459

  • ArcMap 9 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options