Knowledge Base - Technical Articles
HowTo: Add a new field to a shapefile or a feature class using ArcObjects.
| Article ID: | 24817 |
|---|---|
| Software: | ArcGIS - ArcEditor 8.1, 8.1.2, 8.2, 8.3, 9.0 ArcGIS - ArcInfo 8.0.1, 8.0.2, 8.1, 8.1.2, 8.2, 8.3, 9.0 ArcGIS - ArcView 8.1, 8.1.2, 8.2, 8.3, 9.0 |
| Platforms: | Windows 98, NT 4.0, 2000, ME, XP |
Summary
Instructions provide a code sample that shows how to add a new field to a shapefile or featureclass using VBA. The layer that the field will be added to must be selected in the Table of Contents before running the code.
Procedure
- Start ArcMap.
- Open the Visual Basic Editor.
-show me- In ArcMap, select Tools > Macros > Visual Basic Editor. - Expand Project.mxt in the Project Explorer window and select ArcMap Objects > ThisDocument and right-click to select View Code.
-show me-
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 map documents, open the Normal.mxt ThisDocument code module instead.
- Copy the following code into the code module.
Public Sub AddField()
Dim pMxDoc As IMxDocument
Dim pLayer As ILayer
Dim pFLayer As IFeatureLayer
Dim pFeatClass As IFeatureClass
Dim NewField As IFieldEdit
Set pMxDoc = ThisDocument
Set pLayer = pMxDoc.SelectedLayer
Set pFLayer = pLayer
Set pFeatClass = pFLayer.FeatureClass
Set NewField = New Field
With NewField
.Type = 3
.Name = "NewField"
End With
pFeatClass.AddField NewField
End Sub
- Close the Visual Basic Editor.
- Run the code.
-show me- A. Click Tools > Macros > Macros to display the Macros dialog box.
B. Select a macro and click Run.
Created: 4/22/2003
Last Modified: 5/1/2008
By Anonymous - 06/15/2007 8:12 AM
The article is incorrect or the solution didn’t work.
Run-time error '-2147467259 (80004005)': Automation error Unspecified error