How to export attributes to a table


This sample exports the active layer or standalone table in the table of contents to a new dBASE file. The file is written to the c:\temp directory and is named ExpSample. The sample performs the same operation as the export command from the options menu in the table window. If there is a selection or a definition query on the table or layer, it is applied during the export

How to use

  1. Paste the code into VBA.
  2. Select the feature layer or standalone table that you want to export.
  3. The sample will create a new table called ExpSample.dbf in c:\temp directory.Modify the code to specify a different directory or file name.
  4. Run the sample.
[VBA]
Public Sub ExporttoTable()
    
    On Error GoTo EH
    
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    
    ' Get the selected layer or table
    Dim pTable As ITable
    Dim pDispTab As IDisplayTable
    Dim pStAloneTab As IStandaloneTable
    Dim pSelItem As IUnknown
    Set pSelItem = pDoc.SelectedItem
    If pSelItem Is Nothing Then
        MsgBox "No Feature feature layer or table selected"
        Exit Sub
        ' For a feature layer or standalonetable
    ElseIf TypeOf pSelItem Is IFeatureLayer Or TypeOf pSelItem Is IStandaloneTable Then
        Set pDispTab = pSelItem
        Set pTable = pDispTab.DisplayTable
    Else
        MsgBox "Selected item is not a table or feature layer"
        Exit Sub
    End If
    
    ' Get the selection on the table/layer
    Dim pSelSet As ISelectionSet
    Set pSelSet = pDispTab.DisplaySelectionSet
    If pSelSet.Count = 0 Then Set pSelSet = Nothing
    
    ' Get the Definition query (if there is one)
    Dim pTableDef As ITableDefinition
    Dim pQFlt As IQueryFilter
    Set pQFlt = New QueryFilter
    Set pTableDef = pDispTab
    If Not pTableDef.DefinitionExpression = "" Then
        pQFlt.WhereClause = pTableDef.DefinitionExpression
    End If
    
    ' Get the dataset name for the input table
    Dim pDataSet As IDataset
    Dim pDSName As IDatasetName
    Set pDataSet = pTable
    Set pDSName = pDataSet.FullName
    
    ' Get the output dataset name ready. In this
    ' case we are creating a dbf file in c:\temp
    Dim pWkSpFactory As IWorkspaceFactory
    Dim pWkSp As IWorkspace
    Dim pWkSpDS As IDataset
    Dim pWkSpName As IWorkspaceName
    Dim pOutDSName As IDatasetName
    Set pWkSpFactory = New ShapefileWorkspaceFactory
    Set pWkSp = pWkSpFactory.OpenFromFile("c:\temp", 0)
    Set pWkSpDS = pWkSp
    Set pWkSpName = pWkSpDS.FullName
    Set pOutDSName = New TableName
    pOutDSName.Name = "ExpSample11"
    Set pOutDSName.WorkspaceName = pWkSpName
    
    ' Export (Selection is ignored)
    Dim pExpOp As IExportOperation
    Set pExpOp = New ExportOperation
    pExpOp.ExportTable pDSName, pQFlt, pSelSet, pOutDSName, Application.hWnd
    
    ' add the table to map
    Dim pName As IName
    Dim pNewTable
    Dim pStTab As IStandaloneTable
    Dim pStTabColl As IStandaloneTableCollection
    Set pName = pOutDSName
    Set pNewTable = pName.Open
    Set pStTab = New StandaloneTable
    Set pStTab.Table = pNewTable
    Set pStTabColl = pMap
    pStTabColl.AddStandaloneTable pStTab
    
    pDoc.UpdateContents
    
    Exit Sub
EH:
    
    MsgBox Err.Number & " " & Err.Description
    
End Sub