PRJ98: VBA Examples For Programming in Project 98 (180994)



The information in this article applies to:

  • Microsoft Project 98 for Windows

This article was previously published under Q180994

SUMMARY

This article contains examples of programming in Microsoft Visual Basic for Applications, the macro language in Microsoft Project 98.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

MORE INFORMATION

For more information about programming in Visual Basic for Applications in Microsoft Project, while in the Visual Basic Editor, click Contents And Index on the Help menu, click the Contents tab in Project Help, and then double-click the topic you wish to view. If you are unable to find the information you need, ask the Office Assistant.

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

176476 OFF: Office Assistant Not Answering Visual Basic Questions

The macros below assume that at least two tasks exist in your project.

Creating a Macro

To create a macro, follow these steps:

  1. On the Tools menu, point to Macro, and then click Macros.
  2. In the Macro name box, enter a name for the macro, and then click Create to open Visual Basic Editor.
  3. Create a macro by typing any of the subroutines in this article.
Each of the sample macros below begin with the line Sub MacroName() and end with the line End Sub.

Notes on Macro1 through Macro4

Tasks(1) represents the first task in the project. If you want to display the second task's name, replace the 1 with 2. Also, if you need a specific task name, replace the number with the task name. For example, Tasks("MyTask") could replace Tasks(1).

You can replace the Name property that is displayed in the message box with any task property. For example, if you replace Name with Duration, the macro would display the duration of the first task instead of the name.

You can also replace the Name property that is set in the macro with a different property. For example, if you replace Name with Text1 , the task's Text1 field would be set to "Hello World."
  Sub Macro1()
   'This macro displays the first task name, and then sets the Text1
   'field for that task to the string Hello World.

       'This line displays a message box that contains the Task Name
       'of the first task.
       MsgBox ActiveProject.Tasks(1).Name

       'This line sets the value of the Name field to the
       'the string Hello World.
       ActiveProject.Tasks(1).Name = "Hello World"

       'This line sets the value of the Text1 field to the
       'the value of the Name field, which is Hello World.
       ActiveProject.Tasks(1).Text1 = ActiveProject.Tasks(1).Name
   End Sub


   Sub Macro2()
   'This macro loops through all of the tasks in the active project
   'and displays the name of each task.

       For Each x In ActiveProject.Tasks
          MsgBox x.Name
       Next x
   End Sub


   Sub Macro3()
   'This macro loops through all of the tasks in the active project
   ' and sets the Text1 field to the string Hello World.

       For Each x In ActiveProject.Tasks
          x.Text1 = "Hello World"
       Next x
   End Sub


   Sub Macro4()
   'This macro loops through all tasks and adds "Hello " to each task name
   'it also skips summary tasks and blank rows.

   Dim abbr As String
   abbr = "Hello"
   For Each t In ActiveProject.Tasks

     If Not (t Is Nothing) Then
       If Not t.Summary Then
         t.Name = abbr & ": " & t.Name
       End If
     End If
   Next t

   End Sub
				

Notes on Macro5 though Macro7

Macro5 through Macro7 assume that at least two resources exist in your project, and that one is named R1.

Resources(1) represents the first resource in the project. If you want to display the second resource's name, replace the 1 with 2. Also, if you want a specific resource's name, replace the number with the resource name. For example, Resources("R1") could replace Resources(1). If two resources named R1 exist in a project, the statement Resources("R1") would specify the first instance of that resource.
   Sub Macro5()
   'calendarflag is a variable that will be used as a flag
   'to test if the 24 Hours calendar exists in the project.
   calendarflag = 0
   'This loop tests to see if the 24 Hours calendar exists
   'in the ActiveProject.
   For Each C In ActiveProject.BaseCalendars
       If C.Name = "24 Hours" Then calendarflag = 1
   Next C
   'The next line tests if calendarflag is set to 1; if not,
   'then it copies the 24 hour calendar into the ActiveProject.
   If calendarflag = 0 Then
       'This line copies the 24 hour calendar from the Global
       'template into the ActiveProject.
       OrganizerMoveItem Type:=5, FileName:="GLOBAL.MPT", _
       ToFileName:=ActiveProject.Name, Name:="24 Hours"
   End If
   'This line changes the first resources Base Calendar
   'to be the 24 Hours calendar.
   ActiveProject.Resources(1).BaseCalendar = "24 hours"
  End Sub



   Sub Macro6()
   'This macro sets non-working days and alters the working time
   'for a resource calendar.

       'This line sets 1/6/98 through 1/8/98 to be non-working.
       ResourceCalendarEditDays Projectname:=ActiveProject.Name, _
       ResourceName:="R1", StartDate:="1/6/98 12:00 AM", _
       EndDate:="1/8/98 12:00 AM", Working:=False

       'This line changes the working time for the dates 1/12/98
       'through 1/16/98. The new working time is from 7:00 AM to 12:00 PM
       'and 1:00 PM to 7:00 PM.
       ResourceCalendarEditDays Projectname:=ActiveProject.Name, _
       ResourceName:="R1", StartDate:="1/12/98 12:00 AM", _
       EndDate:="1/16/98 12:00 AM", Working:=True, Default:=False, _
       From1:="7:00 AM", To1:="12:00 PM", From2:="1:00 PM", To2:="7:00 PM"

   End Sub


   Sub Macro7()
   'This macro adds a pay rate on a cost table, and then displays the
   'location where it was added.

   'This line adds the pay rate on Cost Rate Table C for 1/15/98 at
   '3.00/hour for the first resource.
   z=activeProject.Resources(1).CostRateTables("c"). _
   PayRates.Add("1/15/1998", "3.00/h")

   'This line displays a message box containing the position where in the
   'collection the rate is added. Payrates are inserted into the
   'collection based on the date. So, if another rate is added with an
   'earlier date than an existing rate, the index for the existing rate
   'would be incremented one higher.
   MsgBox z
   End Sub


   Sub Macro8()
   'This Macro assigns a resource to a task in two different ways.

       'This line assigns the first resource on the Resource Sheet to the
       'first task.
       ActiveProject.Tasks(1).Assignments.Add , 1

       'This line assigns the second resource on the resource sheet to the
       'second task.
       ActiveProject.Resources(2).Assignments.Add 2

   End Sub
				

Notes for Macro9

This macro assumes that resource R1 be assigned to the first task. It also assumes that the task's Start is 1/14/98 and its Duration is 4d.
    Sub Macro9()
   'The following line sets the Actual Work field for Task 1 to 4h on
   '1/15/98.
   ActiveProject.Tasks(1).TimeScaleData(StartDate:= _
   "1/15/98 12:00 AM", EndDate:="1/16/98 12:00 AM", _
   Type:=pjTaskTimescaledActualWork, TimeScaleUnit:=4, _
   Count:=1).Item(1).Value = "4h"

   'The following line sets the Actual Work field for the resource
   'assigned to task 1 to 2h on 1/16/98.
   ActiveProject.Tasks(1).Assignments(1). _
   TimeScaleData(StartDate:="1/16/98 12:00 AM", _
   EndDate:="1/17/98 12:00 AM", Type:=10, TimeScaleUnit:=4, _
   Count:=1).Item(1).Value = "2"
   End Sub
				

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbinfo kbmacro KB180994