Knowledge Base - Technical Articles
HowTo: Programmatically export selected features to a shapefile
| Article ID: | 21610 |
|---|---|
| Software: | ArcGIS - ArcEditor 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 9.0, 9.1, 9.2, 9.3, 9.3.1 |
| Platforms: | N/A |
Summary
This article shows how to export a selected set of features in ArcMap to a new shapefile using VBA and ArcObjects.
Procedure
- Open ArcMap.
- Open the Visual Basic Editor.
-show me- In ArcMap, select Tools > Macros > Visual Basic Editor. - In the Project Explorer window, expand the Project folder and select ThisDocument. Right-click and select View Code.
Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.
- 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 = _
"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
- If necessary, modify the pWorkspaceName.PathName statement to point to a folder where the new shapefile should be created.
- Close the Visual Basic Editor.
- 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.
- Run the code.
-show me- A. Click Tools > Macros > Macros to display the Macros dialog box.
B. 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.
Related Information
- Programmatically export joined features to a shapefile
Instructions provided describe how to export a selected set of features from a joined feature class and table to a new shapefile using VBA and ArcObjects.
Created: 2/11/2002
Last Modified: 5/3/2011
Article Rating:
(2)
By Anonymous - 05/26/2005 8:19 AM
When I execute the ExportFeatureClass subroutine, it fails on line 19: Set pFLayer = pMap.Layer(0) Error Message: Run-time error '13': Type mismatch Is there a problem in the code from the web? or am I missing something else? I'd really like to have this code work so I can automate some shapefile updating! Thanks! Andy Martinez LAN Admin TxDOT, Tyler