Create a new empty shapefile programmatically

Last Published: April 25, 2020


Instructions provided demonstrate how to create a new empty shapefile using ArcObjects.


  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument then right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    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.

  4. Copy the following code into the code module.

    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

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Article ID:000007532

  • ArcMap 9 x
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic