使用VBA返回项目中的过期未完成任务列表

时间:2018-10-03 18:27:26

标签: vba microsoft-project-vba

我正在尝试使用VBA从项目中提取过期的未完成任务列表。直到现在我有了这段代码:

Function GetOverdueTasks() As Tasks
    Dim ProjTasks As Tasks
    Dim ProjTask As Task
    Dim OverdueTasks As Tasks

    Set ProjTasks = ActiveProject.Tasks
    For Each ProjTask In ProjTasks
        If Not (ProjTask Is Nothing) Then
            If ProjTask.Summary = False Then
                If ProjTask.PercentComplete <> 100 And ProjTask.Finish < Now() Then
                    OverdueTasks.Add (Task)
                End If
            End If
        End If
    Next ProjTask
    Set GetOverdueTasks = OverdueTasks
End Function

但是,由于OverdueTasks.Add (Task)对象Tasks未初始化,因此在OverdueTasks行中失败了。但是,我找不到如何初始化Tasks对象。我尝试使用

Set OverdueTasks = New Tasks

但是由于编译错误“无效使用New关键字”而失败。

任何有关此的建议都会很有帮助。

1 个答案:

答案 0 :(得分:3)

您基本上是在寻找一组过期的任务。因此,制作一个Collection

Option Explicit

Private Sub TestFindOverdue()
    Dim overdueTasks As Collection
    Set overdueTasks = GetOverdueTasks

    '--- do something with the overdue tasks here
End Sub

Private Function GetOverdueTasks() As Collection
    Dim ProjTasks As Tasks
    Dim ProjTask As Task
    Dim overdueTasks As Collection

    Set ProjTasks = ActiveProject.Tasks
    For Each ProjTask In ProjTasks
        If Not (ProjTask Is Nothing) Then
            If ProjTask.Summary = False Then
                If ProjTask.PercentComplete <> 100 And ProjTask.Finish < Now() Then
                    overdueTasks.Add ProjTask, ProjTask.UniqueID
                End If
            End If
        End If
    Next ProjTask
    Set GetOverdueTasks = overdueTasks
End Function