Customer Service | Training | Contact Us
Welcome!
Login
Search Options   products areas display

Which products should be considered?

All Products

ArcCAD
ArcEditor
ArcExplorer
ArcGIS Engine
ArcGIS Explorer
ArcGIS Image Server
ArcGIS Mobile
ArcGIS Server
ArcIMS
ArcInfo Desktop
ArcInfo Workstation
ArcLogistics Route
ArcPad
ArcPad Application Builder
ArcReader
ArcSDE
ArcView
ArcView 3.x
ArcWeb Services APIs
ArcWeb Toolbar for ArcGIS
Atlas GIS
BusinessMap
BusinessMap Pro
GIS Portal Toolkit
Job Tracking for ArcGIS
MapIt
Maplex
MapObjects -- Java
MapObjects -- Windows
MapObjects IMS
MapObjects LT
MapStudio
Military Overlay Editor
NetEngine
PC ARC/INFO & DAK
PLTS
RouteMap
RouteMap IMS
SDE
Tracking Server

    Remember these settings for each visit More info
You are here:

Technical Article   HowTo:  Create a new empty shapefile programmatically

Article ID: 28310
Software:  ArcGIS - ArcEditor 8.3, 9.0, 9.1, 9.2, 9.3 ArcGIS - ArcInfo 8.3, 9.0, 9.1, 9.2, 9.3 ArcGIS - ArcView 8.3, 9.0, 9.1, 9.2, 9.3
Platforms:  Windows NT 4.0, 2000, XP

Summary

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

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor. -show me-

    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. -show me-

    [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. -show me-

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

Created: 11/24/2004
Last Modified: 8/26/2008

This website's graphical display is now viewable only with W3C standards-compliant browsers, but the content is accessible to all browsers and Internet devices. View our supported browser matrix for more information on our website display.