如何在 vba 的 Microsoft 项目中查找与滞后和遗漏的关系?

How find relationships with lags and leas in Microsoft project with vba?

我试图通过 Microsoft 项目寻找潜在客户 VBA 但我没有成功 我尝试了以下代码,但它给了我 2280 个潜在客户,而我日程表中的关系总数是 2156

Sub NumberofLeads()

Dim Lead As Integer
Dim t As Task
Dim td As TaskDependency


Lead = 0


For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
      
        
        For Each td In t.TaskDependencies  'looping in all the relations of a task
              If td.Lag < 0 Then
                  Lead = Lead + 1
              End If
        Next
      
         
         
    End If
Next t


MsgBox Lead & " Leads exist."

End Sub

每个 task dependency consists of two tasks so looping through the tasks and then through all of each task's dependencies will encounter each dependency twice. The best way to handle this is to only look at the dependencies where the task is the predecessor (or successor, just pick one), by checking the From (or To) 对象的唯一 ID 并将其与当前任务进行比较:

Sub NumberofLeads()

Dim Lead As Integer
Dim t As Task
Dim td As TaskDependency

Lead = 0

For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
        For Each td In t.TaskDependencies
            If td.From.UniqueID = t.UniqueID And td.Lag < 0 Then
                Lead = Lead + 1
            End If
        Next
    End If
Next t

MsgBox Lead & " Leads exist."

End Sub

注意:如果除以二似乎比此解决方案更容易,请考虑外部链接的情况。在那种情况下,依赖项只会遇到一次(第二个任务不在 ActiveProject.Tasks 集合中),因此除以二会得到错误的答案。