-1

How do you add a summary line for a few tasks in a MS Project schedule dynamically?

Dim ProjTasks = pj.ActiveProject.Tasks

ProjTasks.Add()
ProjTasks(1).name = "Summary Line"
ProjTasks.Add()
ProjTasks(2).name = "Task1"
ProjTasks.Add()
ProjTasks(3).name = "Task2"
ProjTasks.Add()
ProjTasks(4).name = "Task3"

Do you select a bunch of tasks then use the "OutlineIndent" command (seems cumbersome)?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Frank_Vr
  • 661
  • 7
  • 23

1 Answers1

1

Try the code below for a VBA solution, once you select a few tasks (let's say 3) it will add a Summary tasks above them, and indent these 3 tasks inside.

Option Explicit

Sub AddSummayTask()

Dim t               As Task
Dim i               As Long
Dim RowsSelected()  As Long
Dim FirstRow        As Long
FirstRow = 10000

' redim the array of tasks selected to maximum size of 1000
ReDim RowsSelected(0 To 1000)
i = 0

' loop through all selected tasks
For Each t In ActiveSelection.Tasks

    ' add current task ID (row) to an array
    RowsSelected(i) = t.ID

    ' check for the first row
    If FirstRow > t.ID Then FirstRow = t.ID

    i = i + 1
Next t

' redim array of tasks selected to actual size
ReDim Preserve RowsSelected(0 To i - 1)

' add a Task before the first selected row
ActiveProject.Tasks.Add Name:="Summary test", before:=FirstRow

' loop through all rows of selected tasks and indent them
For i = 0 To UBound(RowsSelected)
    ActiveProject.Tasks(RowsSelected(i) + 1).OutlineIndent
Next i

End Sub
Shai Rado
  • 33,032
  • 6
  • 29
  • 51