通过 VBA 添加新值时,MS Project 会更改分配的工作值

发布于 2024-12-20 12:32:42 字数 890 浏览 5 评论 0原文

使用 MS Project 2007 时遇到 VBA 问题。我有一个任务 Task,有 2 个作业,包括资源 Foo(2 天的工作)和 Bar(工作5天)。 任务设置为固定工作。现在,当向任务手动添加额外的任务或从任务中手动删除任务时,一切都按预期工作,没有什么奇怪的事情发生。使用 VBA 时,如下所示,其他作业的工作值会发生变化。

' Adding an Assignment with the "Baz" Resource and 10d of work
Sub AddAssignment()
    Dim tskTask As Task
    Dim rsResource As Resource
    Dim asAssignment As Assignment

    Set tskTask = ActiveProject.Tasks(1)
    Set rsResource = ActiveProject.Resources("Baz")
    Set asAssignment = tskTask.Assignments.Add(tskTask.ID, rsResource.ID)
    asAssignment.Work = "10d"
End Sub

执行脚本之前:

Task:
  Foo 16h
  Bar 40h

执行脚本之后:

Task:
  Foo 9,33h
  Bar 23,33h
  Baz 80h

那么,实际问题是,为了保持其他作业的工作值,我必须做些什么不同的事情?

Got a problem with VBA using MS Project 2007. I have a Task Task with 2 Assignments, including the Resources Foo (2 days of work) and Bar (5 days of work). Task is set to fixed work. Now, when adding additional or removing Assignments manually to/from the Task, all works as expected, nothing fancy happening. When using VBA, like the following, the other Assignments' work values change.

' Adding an Assignment with the "Baz" Resource and 10d of work
Sub AddAssignment()
    Dim tskTask As Task
    Dim rsResource As Resource
    Dim asAssignment As Assignment

    Set tskTask = ActiveProject.Tasks(1)
    Set rsResource = ActiveProject.Resources("Baz")
    Set asAssignment = tskTask.Assignments.Add(tskTask.ID, rsResource.ID)
    asAssignment.Work = "10d"
End Sub

Before executing the script:

Task:
  Foo 16h
  Bar 40h

After executing the script:

Task:
  Foo 9,33h
  Bar 23,33h
  Baz 80h

So, actual question, what do I have to do different in order to keep the other Assignments' work values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-27 12:32:43

实际上更简单的问题是努力驱动的。将任务设置为固定单位并减少工作量,原始代码可以正常工作。

Actually the simpler problem is Effort driven. Set the task to fixed units and effort driven off and the original code works fine.

很酷又爱笑 2024-12-27 12:32:42

由于任务设置为“固定工时”,因此当您添加资源分配时,任务的总工时将保持不变。当您想要添加资源并增加任务的总工作量时,就会出现问题。

在本例中,总工作时间为 56 小时。你添加一个作业,总工时仍然是 56 小时。工作比例相同:40%、100% 和 100%,分别为 9.33 小时、23.33 小时和 23.33 小时。然后,最后一次作业的工时增加到 80 小时,任务的总工时现在为 112.67 小时。

它在用户界面中按照您的预期工作的原因是,您不仅添加了新的分配并设置其工作,而且从 MS Project 的角度来看,您还(重新)设置了现有资源的工作。

在 VBA 中执行此操作的解决方案是准确模拟 UI 中发生的情况,其中包括重置现有资源的工作:

Sub AddAssignment()
    Dim tskTask As Task
    Dim rsResource As Resource
    Dim asAssignment As Assignment
    Dim colAssn As Collection
    Dim iIdx As Integer

    Set tskTask = ActiveProject.Tasks(7)
    Set rsResource = ActiveProject.Resources("Baz")
    Set colAssn = New Collection
    For Each asAssignment In tskTask.Assignments
        colAssn.Add asAssignment.Work
    Next asAssignment
    Set asAssignment = tskTask.Assignments.Add(tskTask.ID, rsResource.ID)
    colAssn.Add 10 * 8 * 60 ' work is stored as minutes
    For iIdx = 1 To colAssn.Count
        tskTask.Assignments(iIdx).Work = colAssn(iIdx)
    Next iIdx
End Sub

Because the task is set to Fixed Work, when you add a resource assignment, it will keep the total work for the task the same. The problem arises when you want to add a resource and increase the total amount of work for the task.

What's happening in this case is that the total work was 56h. You add an assignment and the total work is still 56h. The ratio of the work is the same: 40%, 100%, and 100%, which works out to 9.33h, 23.33h, and 23.33h. Then the work for the last assignment is increased to 80h and the total work for the task is now 112.67h.

The reason it works as you intend in the user-interface is because you are not just adding a new assignment and setting its work, but from the perspective of MS Project, you are also (re)setting the work on the existing resources.

The solution to doing this in VBA is to emulate exactly what is happening in the UI--which includes resetting the work for the existing resources:

Sub AddAssignment()
    Dim tskTask As Task
    Dim rsResource As Resource
    Dim asAssignment As Assignment
    Dim colAssn As Collection
    Dim iIdx As Integer

    Set tskTask = ActiveProject.Tasks(7)
    Set rsResource = ActiveProject.Resources("Baz")
    Set colAssn = New Collection
    For Each asAssignment In tskTask.Assignments
        colAssn.Add asAssignment.Work
    Next asAssignment
    Set asAssignment = tskTask.Assignments.Add(tskTask.ID, rsResource.ID)
    colAssn.Add 10 * 8 * 60 ' work is stored as minutes
    For iIdx = 1 To colAssn.Count
        tskTask.Assignments(iIdx).Work = colAssn(iIdx)
    Next iIdx
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文