batch_add_field_VBA
- by Elaine Kuo
I am unsure where goes wrong.
Please kindly help and thanks.
Public Sub AddField()
Dim pApp As esriCatalogUI.IGxApplication
Set pApp = esriArcCatalog.Application
Dim pGxSelection As esriCatalog.IGxSelection
Set pGxSelection = pApp.Selection
Dim plist As esriCatalog.IEnumGxObject
Set plist = pGxSelection.SelectedObjects
Dim pGxObject As esriCatalog.IGxObject
Dim pName As esriSystem.IName
Dim pDS As esriGeoDatabase.IDataset
Dim pGDSE As esriGeoDatabase.IGeoDatasetSchemaEdit
Dim pStatusBar As esriSystem.IStatusBar
Set pStatusBar = esriArcCatalog.Application.StatusBar
Dim pFeatLyr As esriCarto.IFeatureLayer
Dim pFeatureClass As esriGeoDatabase.IFeatureClass
Dim pFeatureDataset As esriGeoDatabase.IFeatureDataset
Dim pFieldEdit As esriGeoDatabase.IFieldEdit
Set pGxObject = plist.Next
Set pFeatureClass = pGxObject
If pFeatureClass.Type = esriDTFeatureClass Then
Set pFeatLyr = New FeatureLayer
Set pFeatLyr.FeatureClass = pFeatureDataset.Dataset
pFeatLyr.name = pGXDataset.Dataset.name
End If
'Checks to make sure you have something selected
If pGxObject Is Nothing Then
MsgBox "You need to select the files", vbOKOnly, "Error"
Exit Sub
End If
'Runs a function to add field
Dim pField1 As esriGeoDatabase.IFieldEdit
' Define the first new field.
Set pField1 = New Field
pField1.Name = "GID1"
pField1.Type = esriFieldTypeInteger
pField1.Length = 10
pFeatureClass.AddField pField1
'Loops through all selected files and preforms
Do Until pGxObject Is Nothing
If TypeOf pGxObject Is esriCatalog.IGxDataset Then
Set pName = pGxObject.InternalObjectName
Set pDS = pName.Open
Set pGDSE = pDS
With pGDSE
If .CanAlterSpatialReference Then
.AlterSpatialReference pFieldEdit
End If
End With
End If
pStatusBar.Message(0) = "Done: " & pGxObject.Name
Set pGxObject = plist.Next
Loop
CleanUp:
Set pFieldEdit = Nothing
Set pGDSE = Nothing
pStatusBar.Message(0) = "Done"
End Sub