Search code examples
vbams-project

Using a variable instead of the fieldname


I have 4 Custom fields in Microsoft project. "Optimistic", "MostLikely" and "Pessimistic" are required CustomNames for any 3 of the Duration fields. "CriticalCnt" is also a required Custom Name for any of the 20 Number fields. I have a routine which identifies the Microsoft Project Fieldname which has been customized which I have assigned to a variable. When I am looking for the values for these fields for each task, I want to use the variable to identify the field.

I have researched the Web for "ProjectSummaryTask","FieldConstantToFieldname",FieldnametoFieldConstant" with no luck.

What I want to do is to do the following:

Dim i as integer, fieldvalue as long, Tsk as Task
For I = 1 to ActiveProject.task.count do
    Fieldvalue =Activeproject.Task.CriticalCnt
    Debug.print ("Task=" & Task(i).id & " Task(i).CriticalCnt=" & 
    Task(i).CriticalCnt)
next i

This routine gets the 9 digit code to identify the CustomNamed field. In this case, Number4 was assigned the CustomName of "CriticalCnt". I have no idea what is being updated in this routine with the Value 3.

Sub GetFieldID()

Dim Projectfield As Long, ProjectFieldName As String, Message As String, i 
As Integer, tasks As tasks, t As Long, NT As Long, tmp As tasks, criticalCnt 
As String

Projectfield = FieldNameToFieldConstant("CriticalCnt", pjDuration)
Message = "ProjectfieldNumber = " & Projectfield & vbCrLf
MsgBox Message & ActiveProject.ProjectSummaryTask.GetField(Projectfield)
ActiveProject.ProjectSummaryTask.SetField FieldID:=Projectfield, Value:="3"
ProjectFieldName = FieldConstantToFieldName(Projectfield)
Message = "New Value for Field:" & ProjectFieldName & vbCrLf
MsgBox Message & ActiveProject.ProjectSummaryTask.GetField(Projectfield)
Set tasks = ActiveProject.tasks
NT = tasks.Count

Debug.Print ("NT=" & NT)
    For t = 1 To NT
        Debug.Print ("T=" & t)
        Debug.Print (".id = " & tasks(t).ID & " Value= " & Projectfield & _
        " tasks(t).Projectfield= " & Projectfield)
        Debug.Print ("Value=" & _
        ActiveProject.ProjectSummaryTask.GetField(Projectfield))
    Next t
End Sub

Solution

  • This function finds the number field customized with the name "CriticalCnt" and uses it to identify which of the three customized duration fields contains the correct duration for each task and outputs each task's ID with its custom duration. Note that CriticalCnt contains the field ID of the customized duration field (a nine-digit number).

    Sub GetCustomDuration()
    
    ' Schedule contains 4 customized fields--1 number field and 3 duration fields.
    ' The duration fields are named "Optimistic", "MostLikely" and "Pessimistic".
    ' Each task uses one of these fields and the selected one is identified in the
    ' customized number field called "CriticalCnt" which contains its field ID.
    
    
    ' find the field customized to contain the CriticalCnt data
    Dim fldCriticalCnt As Long
    fldCriticalCnt = Application.FieldNameToFieldConstant("CriticalCnt")
    
    Dim tsk As Task
    For Each tsk In ActiveProject.Tasks
    
        ' determine which of the three duration fields to use for this task
        Dim fldCustomDur As Long
        fldCustomDur = CLng(tsk.GetField(fldCriticalCnt))
    
        ' get the custom duration value
        Dim CustomDur As String
        CustomDur = tsk.GetField(fldCustomDur)
    
        Debug.Print "Task=" & tsk.ID & " CriticalCnt=" & CustomDur _
            & " (" & DurationValue(CustomDur) & " minutes)"
    Next tsk
    
    End Sub
    

    Note: It would seem that storing the name of the custom duration field rather than the field ID would be better for end-user. In this case CriticalCnt would be a text field customized with a drop-down list of three items: "Optimistic", "MostLikely" and "Pessimistic".