+ Reply to Thread
Results 1 to 4 of 4

Thread: how to modify VBA as python

  1. #1
    Elaine Kuo
    Join Date
    Apr 2010
    Posts
    204
    Points
    0
    Answers Provided
    3


    0

    Default how to modify VBA as python

    Hello,

    I created a VBA to generate a new field (A_new)(season) of polygon and then copy an existing field (A_old) to it.
    (For ArcInfo 9.3)

    However, there are 500 shapefiles waiting for this task.
    The file names are different but share the beginning of C. (For instance, C9568, C4803, and C3208)
    Please kindly advise if there is any VBA code to copy the existing fields (C9568, C4803, and C3208)
    to the new field with the same name (season) without doing it for 500 times.
    Thanks a lot.


    Code

    Public Sub CreateField()
    On Error GoTo eh:

    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    Dim pLayer As ILayer
    Set pLayer = pMap.Layer(0)
    Dim pFeatureLayer As IFeatureLayer
    Dim pFeatureClass As IFeatureClass
    If TypeOf pLayer Is IFeatureLayer Then
    Set pFeatureLayer = pLayer
    Else
    Exit Sub
    End If
    Set pFeatureClass = pFeatureLayer.FeatureClass
    Dim pField As IField
    Dim pFieldedit As IFieldEdit
    Set pField = New Field
    Set pFieldedit = pField
    With pFieldedit
    .Name = "Season"
    .AliasName = "Season"
    .Type = esriFieldTypeString
    .Length = 6
    End With
    pFeatureClass.AddField pField
    Call UpdateFeatures
    eh:
    End Sub

    Public Sub UpdateFeatures()
    Dim pFeatureClass As IFeatureClass
    Dim pFeatureLayer As IFeatureLayer
    Dim pDoc As IMxDocument
    Dim pMap As IMap

    Set pDoc = ThisDocument
    Set pMap = pDoc.Maps.Item(0)
    Set pFeatureLayer = pMap.Layer(0)
    Set pFeatureClass = pFeatureLayer.FeatureClass

    Dim pFeatureCursor As IFeatureCursor
    Dim pFeature As IFeature

    Set pFeatureCursor = pFeatureClass.Update(Nothing, False)

    '++++++ Loop through each feature and update
    Set pFeature = pFeatureCursor.NextFeature
    Do While Not pFeature Is Nothing
    pFeature.Value(pFeature.Fields.FindField("Season")) = pFeature.Value(pFeature.Fields.FindField("C9469"))
    pFeature.Store
    Set pFeature = pFeatureCursor.NextFeature
    Loop
    End Sub

  2. #2
    Darren Wiens

    Join Date
    Apr 2011
    Posts
    821
    Points
    271
    Answers Provided
    37


    0
    This post is marked as the answer

    Default Re: how to modify VBA as python

    In Python:
    - List feature classes
    - Loop through each feature class in the list
    - Add a field
    - Calculate the field

  3. #3
    Elaine Kuo
    Join Date
    Apr 2010
    Posts
    204
    Points
    0
    Answers Provided
    3


    0

    Default Re: how to modify VBA as python

    Thanks a lot.
    It saves a lot of time

  4. #4
    Tony Almeida
    Join Date
    Apr 2010
    Posts
    285
    Points
    5
    Answers Provided
    1


    0

    Default Re: how to modify VBA as python

    Helenak,

    Could you post your code please.
    I would like to see how you accomplished converting your vba code to python.
    I am working on the same thing.

    Thanks.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts