PRJ: How to Use the GanttBarFormat Method

ID: Q126943

The information in this article applies to:

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" (without the quotation mark s).

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.

   Sub Macro2()

   SelectBeginning

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

   End Sub

REFERENCES

For more information on the GanttBarFormat method search on the word "Gantt" (without the quotation marks) in the Visual Basic Reference Help.

Additional query words: 4.00

Keywords          : kbcode kbprg
Version           : 4.0 4.1 4.1a 98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: November 25, 1997