Microsoft KB Archive/126943

= PRJ: How to Use the GanttBarFormat Method =

Article ID: 126943

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Project 98 Standard Edition
 * Microsoft Project 4.1 Standard Edition
 * Microsoft Project 4.1a
 * Microsoft Project 4.0 Standard Edition
 * Microsoft Project 4.0 for Macintosh

-



This article was previously published under Q126943



SUMMARY
This article discusses two ways you can use the GanttBarFormat method in a macro that loops through a set of tasks and formats them based on a particular set of criteria.



MORE INFORMATION
The GanttBarFormat method is used to change the format of the Gantt Bars from the default styles that have been applied using the Bar Styles command on the Format menu. By default, the GanttBarFormat method applies to all selected tasks.

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

Below are two examples of macros that loop through tasks in the active project and format those tasks that have a task name of a with a left diagonal bar pattern and those that don't with a crossed line bar pattern.

To test these macros, create a project with three or four tasks with two of the tasks having a task name of a.

NOTE: The Gantt Chart View must be active when you run these macros or you will receive a run-time error.

Sample Macro 1
Including the TaskID named argument with the GanttBarFormat method allows the macro to cycle through the tasks in the active project without having to manually select them using a "SelectCellDown" or other equivalent command. The following macro demonstrates this procedure: Sub Macro1

Dim t As Task

For Each t In ActiveProject.Tasks

'skip over blank rows If Not t is Nothing then If (t.Name = "a") Then GanttBarFormat TaskID:=t.ID, MiddlePattern:=5 Else GanttBarFormat TaskID:=t.ID, MiddlePattern:=10 End If        End If      Next t   End Sub

Sample Macro 2
The following example selects each task in the active project (using the SelectCellDown method), tests the name field, and then applies the Gantt bar formatting. The macro stops when a blank task is encountered. This macro is slower than Sample Macro 1 because of the additional work associated with the selection process. It is also more error prone because it depends on a certain table layout and assumes that the "ActiveCell" is the name field.

NOTE: This macro assumes that there are no blank rows between real tasks. SelectBeginning

Do Until (ActiveCell = "") If ActiveCell.Task.Name = "a" Then GanttBarFormat MiddlePattern:=5 Else GanttBarFormat MiddlePattern:=10 End If          SelectCellDown Loop

End Sub

