R
RNobleman
I've written a macro to format the Gantt bar based on a custom
resource field that contains various colors. The macro works fine on
individual projects, but not in a master project. When I run it, I get
the following error: "Argument not valid" on the FormatGanttBar line.
There are only two arguments on that line: TaskID and MiddleColor.
Here is my code. Any suggestions of how to make it work for a Master
Project would be much appreciated. Thanks.
Sub Format_Gantt_Bar_by_Resource()
Dim myTask As Task
Dim ResourceID As Long
Dim ColorToApply As String
For Each myTask In ActiveProject.Tasks
If Not myTask Is Nothing Then 'check Task Is Not blank /
’missing’
If Not myTask.ExternalTask Then
If Not myTask.Summary And Not myTask.ResourceNames = ""
Then ' Not a summary task or blank resource field
ResourceID = myTask.Resources(1)
ID = FieldNameToFieldConstant("Gantt Color", pjResource)
ColorToApply =
ActiveProject.Resources(ResourceID).GetField(ID)
If ColorToApply = "Red" Then
ColorNumber = pjRed
ElseIf ColorToApply = "Black" Then
ColorNumber = pjBlack
ElseIf ColorToApply = "Blue" Then
ColorNumber = pjBlue
ElseIf ColorToApply = "Fuchsia" Then
ColorNumber = pjFuchsia
ElseIf ColorToApply = "Gray" Then
ColorNumber = pjGray
ElseIf ColorToApply = "Green" Then
ColorNumber = pjGreen
ElseIf ColorToApply = "Lime" Then
ColorNumber = pjLime
ElseIf ColorToApply = "Maroon" Then
ColorNumber = pjMaroon
ElseIf ColorToApply = "Navy" Then
ColorNumber = pjNavy
ElseIf ColorToApply = "Olive" Then
ColorNumber = pjOlive
ElseIf ColorToApply = "Purple" Then
ColorNumber = pjPurple
ElseIf ColorToApply = "Silver" Then
ColorNumber = pjSilver
ElseIf ColorToApply = "Teal" Then
ColorNumber = pjTeal
ElseIf ColorToApply = "White" Then
ColorNumber = pjWhite
ElseIf ColorToApply = "Yellow" Then
ColorNumber = pjYellow
Else
ColorNumber = -1
End If
If ColorNumber <> -1 Then
'Apply color to the Gantt Chart
GanttBarFormat TaskID:=myTask.ID,
MiddleColor:=ColorNumber
'Success
End If
End If 'myTask Is Summary
End If 'myTask Is Nothing
End If 'myTask.ExternalTask
Next
End Sub
resource field that contains various colors. The macro works fine on
individual projects, but not in a master project. When I run it, I get
the following error: "Argument not valid" on the FormatGanttBar line.
There are only two arguments on that line: TaskID and MiddleColor.
Here is my code. Any suggestions of how to make it work for a Master
Project would be much appreciated. Thanks.
Sub Format_Gantt_Bar_by_Resource()
Dim myTask As Task
Dim ResourceID As Long
Dim ColorToApply As String
For Each myTask In ActiveProject.Tasks
If Not myTask Is Nothing Then 'check Task Is Not blank /
’missing’
If Not myTask.ExternalTask Then
If Not myTask.Summary And Not myTask.ResourceNames = ""
Then ' Not a summary task or blank resource field
ResourceID = myTask.Resources(1)
ID = FieldNameToFieldConstant("Gantt Color", pjResource)
ColorToApply =
ActiveProject.Resources(ResourceID).GetField(ID)
If ColorToApply = "Red" Then
ColorNumber = pjRed
ElseIf ColorToApply = "Black" Then
ColorNumber = pjBlack
ElseIf ColorToApply = "Blue" Then
ColorNumber = pjBlue
ElseIf ColorToApply = "Fuchsia" Then
ColorNumber = pjFuchsia
ElseIf ColorToApply = "Gray" Then
ColorNumber = pjGray
ElseIf ColorToApply = "Green" Then
ColorNumber = pjGreen
ElseIf ColorToApply = "Lime" Then
ColorNumber = pjLime
ElseIf ColorToApply = "Maroon" Then
ColorNumber = pjMaroon
ElseIf ColorToApply = "Navy" Then
ColorNumber = pjNavy
ElseIf ColorToApply = "Olive" Then
ColorNumber = pjOlive
ElseIf ColorToApply = "Purple" Then
ColorNumber = pjPurple
ElseIf ColorToApply = "Silver" Then
ColorNumber = pjSilver
ElseIf ColorToApply = "Teal" Then
ColorNumber = pjTeal
ElseIf ColorToApply = "White" Then
ColorNumber = pjWhite
ElseIf ColorToApply = "Yellow" Then
ColorNumber = pjYellow
Else
ColorNumber = -1
End If
If ColorNumber <> -1 Then
'Apply color to the Gantt Chart
GanttBarFormat TaskID:=myTask.ID,
MiddleColor:=ColorNumber
'Success
End If
End If 'myTask Is Summary
End If 'myTask Is Nothing
End If 'myTask.ExternalTask
Next
End Sub