HOW TO
Instructions provided demonstrate how to create a new empty shapefile using ArcObjects.
Note:
Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt ThisDocument code module instead.
Code:
Sub CreateNewEmptyShapefile_Point()
Const strFolder As String = "C:\temp"
Const strName As String = "NewShapeFile" ' Edit as needed. Don't include .shp extension
Const strShapeFieldName As String = "Shape"
' Open the folder to contain the shapefile as a workspace
Dim pFWS As IFeatureWorkspace
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Set pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)
' Set up a simple fields collection
Dim pFields As IFields
Dim pFieldsEdit As IFieldsEdit
Set pFields = New Fields
Set pFieldsEdit = pFields
Dim pField As IField
Dim pFieldEdit As IFieldEdit
' Make the shape field
' it will need a geometry definition, with a spatial reference
Set pField = New Field
Set pFieldEdit = pField
pFieldEdit.Name = strShapeFieldName
pFieldEdit.Type = esriFieldTypeGeometry
Dim pGeomDef As IGeometryDef
Dim pGeomDefEdit As IGeometryDefEdit
Set pGeomDef = New GeometryDef
Set pGeomDefEdit = pGeomDef
With pGeomDefEdit
'Uncomment one line and comment the other two depending on type of geometry
'to be created -- default is point
.GeometryType = esriGeometryPoint 'Creates point shapefile
'.GeometryType = esriGeometryPolyline 'Creates polyline shapefile
'.GeometryType = esriGeometryPolygon 'Creates polygon shapefile
Set .SpatialReference = New UnknownCoordinateSystem
'or comment line above and uncomment 5 lines below (and modify accordingly) to
'set spatial reference
''Set spatial reference for the new shapefile
' Dim pSpatRefFact As ISpatialReferenceFactory2
' Set pSpatRefFact = New SpatialReferenceEnvironment
'
' Dim pGeoCoordSys As IProjectedCoordinateSystem
' Set pGeoCoordSys = pSpatRefFact.CreateProjectedCoordinateSystem(esriSRProjCS_NAD1983N_AmericaLambert)
'
' Set .SpatialReference = pGeoCoordSys
End With
Set pFieldEdit.GeometryDef = pGeomDef
pFieldsEdit.AddField pField
' Add another miscellaneous text field
Set pField = New Field
Set pFieldEdit = pField
With pFieldEdit
.Length = 25
.Name = "TextField"
.Type = esriFieldTypeString
End With
pFieldsEdit.AddField pField
' Create the shapefile
Dim pFeatClass As IFeatureClass
Set pFeatClass = pFWS.CreateFeatureClass(strName, pFields, Nothing, _
Nothing, esriFTSimple, strShapeFieldName, "")
End Sub
Article ID:000007532
Get help from ArcGIS experts
Download the Esri Support App