Loop through each field for MS Project

2020-04-17 05:22发布

问题:

I am trying to loop through each field in my MS Project file and gather information about that field (custom name is particular). What is the easiest way to do this?

Thanks

回答1:

To get the custom name of a field you'll need the field constant and to loop through all fields, you'll need to store a list of all field constants.

Here is a simple example to get you started. I hard-coded an array of the field constants for the Task Text1-30 fields.

Sub GetCustomFieldNames()

    Dim TextFields As Variant
    TextFields = Array(188743731, 188743734, 188743737, 188743740, 188743743 _
        , 188743746, 188743747, 188743748, 188743749, 188743750, 188743997, 188743998 _
        , 188743999, 188744000, 188744001, 188744002, 188744003, 188744004, 188744005 _
        , 188744006, 188744007, 188744008, 188744009, 188744010, 188744011, 188744012 _
        , 188744013, 188744014, 188744015, 188744016)

    Dim FldID As Variant

    For Each FldID In TextFields
        If Len(CustomFieldGetName(FldID)) > 0 Then
            Debug.Print FieldConstantToFieldName(FldID), CustomFieldGetName(FldID)
        End If
    Next FldID

End Sub

Here are a few ways to improve on this:

  • Replace the numeric values with the enumerated constant (e.g. pjTaskText1)
  • Store the list of fields in an external file (e.g. a csv file that contains the field name, field constant, etc.)
  • If your macro is stored in a macro-only project file, you can store the fields in the resource sheet and loop through them by looping through the resources (see screen shot).