Programmatically export selected rows from a stand-alone table to another

Last Published: April 25, 2020


Instructions provided describe how to export selected rows from a stand-alone table to a new stand-alone table using ArcObjects.


  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 then 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 the map documents, open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module.

    Sub ExportSelectedRowsTable()

    ' get table collection of focus data frame
    ' make reference to the top table in TOC
    Dim pMxDoc As IMxDocument
    Set pMxDoc = Application.Document
    Dim pMap As IStandaloneTableCollection
    Set pMap = pMxDoc.FocusMap
    Dim pStdAloneTbl As IStandaloneTable
    Set pStdAloneTbl = pMap.StandaloneTable(0)

    'Get the Table Name and pass it to Dataset Name
    Dim pTable As ITable
    Set pTable = pStdAloneTbl.Table
    Dim pDataset As IDataset
    Set pDataset = pTable
    Dim pInTableName As ITableName
    Set pInTableName = pDataset.FullName
    Dim pInDsName As IDatasetName
    Set pInDsName = pInTableName

    ' get selection set from table
    Dim pTableSel As ITableSelection
    Set pTableSel = pStdAloneTbl
    Dim pSelSet As ISelectionSet
    Set pSelSet = pTableSel.SelectionSet

    ' create a new output table name and pass it to dataset name
    Dim pOutTableName As ITableName
    Set pOutTableName = New TableName
    Dim pOutDSName As IDatasetName
    Set pOutDSName = pOutTableName
    pOutDSName.Name = "TestExport.dbf" ' Define the name

    ' specify output workspace and pass it to Output Dataset Name
    Dim pWorkspaceName As IWorkspaceName
    Set pWorkspaceName = New WorkspaceName
    pWorkspaceName.PathName = "c:\temp"
    pWorkspaceName.WorkspaceFactoryProgID = "esriCore.shapefileworkspacefactory.1"
    Set pOutDSName.WorkspaceName = pWorkspaceName

    ' export table
    Dim pExportOp As IExportOperation
    Set pExportOp = New ExportOperation
    pExportOp.ExportTable pInDsName, Nothing, pSelSet, pOutDSName, 0

    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:000008745

  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic