Frank_Vr Frank_Vr - 1 year ago 96 Question

Add a summary line in MS Project

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

Dim ProjTasks = pj.ActiveProject.Tasks

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

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download