在MS Project中添加摘要行

时间:2016-11-23 07:05:08

标签: ms-project microsoft-project-vba

如何动态地为MS Project计划中的几个任务添加摘要行?

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"

您是否选择了一堆任务然后使用“OutlineIndent”命令(看起来很麻烦)?

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码获取VBA解决方案,一旦您选择了一些任务(让我们说3),它将在它们上面添加一个摘要任务,并在里面缩进这3个任务。

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