Programmatically export selected features to a shapefile

Last Published: February 12, 2024


This article shows how to export a selected set of features in ArcMap to a new shapefile using VBA and ArcObjects.

The information in this article applies to ArcGIS versions 9.x only. Visual Basic for Applications (VBA) for ArcMap and ArcCatalog has been deprecated at ArcGIS 10.


  1. Open ArcMap.
  2. Open the Visual Basic Editor.
  • In ArcMap, select Tools > Macros > Visual Basic Editor.
  1. In the Project Explorer window, expand the Project folder and select ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Code in ThisDocument code module only runs in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module. 
  2. Paste the following code into the code module.
    Sub ExportFeatureClass()
        Dim pDoc As IMxDocument
        Dim pMap As IMap
        Dim pFLayer As IFeatureLayer
        Dim pFc As IFeatureClass
        Dim pINFeatureClassName As IFeatureClassName
        Dim pDataset As IDataset
        Dim pInDsName As IDatasetName
        Dim pFSel As IFeatureSelection
        Dim pSelSet As ISelectionSet
        Dim pFeatureClassName As IFeatureClassName
        Dim pOutDatasetName As IDatasetName
        Dim pWorkspaceName As IWorkspaceName
        Dim pExportOp As IExportOperation
        Set pDoc = ThisDocument
        Set pMap = pDoc.FocusMap
        Set pFLayer = pMap.Layer(0)
        Set pFc = pFLayer.FeatureClass
        'Get the FcName from the featureclass
        Set pDataset = pFc
        Set pINFeatureClassName = pDataset.FullName
        Set pInDsName = pINFeatureClassName
        'Get the selection set
        Set pFSel = pFLayer
        Set pSelSet = pFSel.SelectionSet
        'Define the output feature class name
        Set pFeatureClassName = New FeatureClassName
        Set pOutDatasetName = pFeatureClassName
        pOutDatasetName.Name = "TestExport"
        Set pWorkspaceName = New WorkspaceName
        pWorkspaceName.PathName = "c:\temp"
        pWorkspaceName.WorkspaceFactoryProgID = _
        Set pOutDatasetName.WorkspaceName = pWorkspaceName
        pFeatureClassName.FeatureType = esriFTSimple
        pFeatureClassName.ShapeType = esriGeometryAny
        pFeatureClassName.ShapeFieldName = "Shape"
        Set pExportOp = New ExportOperation
        pExportOp.ExportFeatureClass pInDsName, Nothing, _
               pSelSet, Nothing, pOutDatasetName, 0
    End Sub
  3. If necessary, modify the pWorkspaceName.PathName statement to point to a folder where the new shapefile should be created.
  4. Close the Visual Basic Editor.
  5. Add a feature class to ArcMap and select some features to export.
    The first layer in the map must be a vector layer, otherwise a "type mismatch" error will occur.
  6. Run the code.
    1. Click Tools > Macros > Macros to display the Macros dialog box.
    2. Select a macro and click Run. Look in the folder you specified in the PathName setting to see the shapefiles created. Add the shapefile to the map document to see the features that were created.

Article ID:000004964

  • 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

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options