none
Detecting Cost Resources RRS feed

  • Question

  • I have a VBA macro that needs to detect cost resources. When it encounters a task that does not have an assigned resource, an error occurs (The argument value is not valid.). Is there a way to detect cost resources without triggering this error? Here is the code I use to detect tasks with a cost resource:

                    If t.Assignments(1).ResourceType = pjResourceTypeCost Then
                    'do stuff
                    Else
                       'do other stuff
                    End If

    Sunday, April 21, 2013 3:02 AM

Answers

  • Hello RobVVV

    The line If. t.Assignments(1)... indicates that there is a assignments.

    First ask how many assignents the task has.

    if t.Assignments.count > 0 ...

    Then loop through the assignments because a task can have more then one.

    Foreach asn in t.Assignments

    ' do stuff

    next asn

    Regards

    • Marked as answer by RobVVV Sunday, April 21, 2013 11:23 PM
    Sunday, April 21, 2013 9:35 AM
  • RobVVV,

    You get an error when the code attempts to directly address a task with no assignments. The "classic" way to format this structure is similar to the following.

    Sub CostType()
    Dim t As Task
    Dim a As Assignment
    For Each t In ActiveProject.Tasks
        If Not t Is Nothing Then
            For Each a In t.Assignments
                If .ResourceType = pjResourceTypeCost Then
                    'do stuff
                Else
                    'do other stuff
                End If
            Next a
        End If
    Next t
    End Sub

    John

    Sunday, April 21, 2013 4:03 PM

All replies

  • Hello RobVVV

    The line If. t.Assignments(1)... indicates that there is a assignments.

    First ask how many assignents the task has.

    if t.Assignments.count > 0 ...

    Then loop through the assignments because a task can have more then one.

    Foreach asn in t.Assignments

    ' do stuff

    next asn

    Regards

    • Marked as answer by RobVVV Sunday, April 21, 2013 11:23 PM
    Sunday, April 21, 2013 9:35 AM
  • RobVVV,

    You get an error when the code attempts to directly address a task with no assignments. The "classic" way to format this structure is similar to the following.

    Sub CostType()
    Dim t As Task
    Dim a As Assignment
    For Each t In ActiveProject.Tasks
        If Not t Is Nothing Then
            For Each a In t.Assignments
                If .ResourceType = pjResourceTypeCost Then
                    'do stuff
                Else
                    'do other stuff
                End If
            Next a
        End If
    Next t
    End Sub

    John

    Sunday, April 21, 2013 4:03 PM
  • Thanks CadmusB and John. I appreciate the help.
    Sunday, April 21, 2013 11:23 PM
  • RobVVV,

    You're welcome and thanks for the feedback.

    John

    Monday, April 22, 2013 1:53 AM