HOW TO
This article shows how to export a selected set of features in ArcMap to a new shapefile using VBA and ArcObjects.
Note: 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.
Note: 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.
Code: 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 = _ "esriCore.shapefileworkspacefactory.1" Set pOutDatasetName.WorkspaceName = pWorkspaceName pFeatureClassName.FeatureType = esriFTSimple pFeatureClassName.ShapeType = esriGeometryAny pFeatureClassName.ShapeFieldName = "Shape" 'Export Set pExportOp = New ExportOperation pExportOp.ExportFeatureClass pInDsName, Nothing, _ pSelSet, Nothing, pOutDatasetName, 0 End Sub
Note: The first layer in the map must be a vector layer, otherwise a "type mismatch" error will occur.
Get help from ArcGIS experts
Download the Esri Support App