Create a new shapefile

Last Modified:6/21/2001

This sample will demonstrate the steps needed to create a new shapefile.

How to use:
  1. Paste the code into your VB or VBA Application.
  2. Call the function from within your application.
Public Sub CreateShapefile()

  Const strFolder As String = "D:\Data"
  Const strName As String = "MyShapeFile" ' Dont 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 esriCore.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 esriCore.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
    .GeometryType = esriGeometryPolygon
    Set .SpatialReference = New UnknownCoordinateSystem
  End With
  Set pFieldEdit.GeometryDef = pGeomDef
  pFieldsEdit.AddField pField

  ' Add another miscellaneous text field
  Set pField = New esriCore.Field
  Set pFieldEdit = pField
  With pFieldEdit
      .Length = 30
      .Name = "MiscText"
      .Type = esriFieldTypeString
  End With
  pFieldsEdit.AddField pField
  ' Create the shapefile
  ' (some parameters apply to geodatabase options and can be defaulted as Nothing)
  Dim pFeatClass As IFeatureClass
  Set pFeatClass = pFWS.CreateFeatureClass(strName, pFields, Nothing, _
                                           Nothing, esriFTSimple, strShapeFieldName, "")
End Sub