将 Task.Duration 分配给 VBA Microsoft Project 中已添加的任务

发布于 2024-09-25 15:59:46 字数 958 浏览 12 评论 0原文

我目前正在开发一个 VBA 宏,将产品需求导入到 Microsoft Project 中。

我使用以下代码来添加/更新任务:

Function AddTask(strText As String, lngDuration As Long, taskParent As Task)
    Dim oldTask As Task
    Set oldTask = taskParent.OutlineChildren(strText)
    If oldTask Is Nothing Then
        Dim newTask As Task
        Set newTask = taskParent.OutlineChildren.Add(Name:=strText, Before:=LastIndexOf(taskParent) + 1)
        newTask.OutlineLevel = taskParent.OutlineLevel + 1
        newTask.Duration = lngDuration
        Set AddTask = newTask
    Else
        oldTask.Duration = lngDuration
        Set AddTask = oldTask
    End If
End Function

这对于新任务来说非常有效,但不幸的是,当我尝试更新旧任务的 Duration 属性时,我收到了一个奇怪的错误。

Run-Time Error '1101'

Argument value is not valid

我真的不明白

newTask.Duration = lngDuration

oldTask.Duration = lngDuration

之间有什么区别?
请帮忙!

I'm currently working on a VBA macro importing product-requirements into Microsoft Project.

I use the following code for adding/updating a task:

Function AddTask(strText As String, lngDuration As Long, taskParent As Task)
    Dim oldTask As Task
    Set oldTask = taskParent.OutlineChildren(strText)
    If oldTask Is Nothing Then
        Dim newTask As Task
        Set newTask = taskParent.OutlineChildren.Add(Name:=strText, Before:=LastIndexOf(taskParent) + 1)
        newTask.OutlineLevel = taskParent.OutlineLevel + 1
        newTask.Duration = lngDuration
        Set AddTask = newTask
    Else
        oldTask.Duration = lngDuration
        Set AddTask = oldTask
    End If
End Function

This works perfectly for a new task, but unfortunately I get a weird error when trying to update the Duration property on an old task.

Run-Time Error '1101'

Argument value is not valid

I really don't understand what the difference between

newTask.Duration = lngDuration

and

oldTask.Duration = lngDuration

What's going on here?
Please help!

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

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

发布评论

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

评论(1

冧九 2024-10-02 15:59:46

我自己找到的!

问题是有时任务会被添加到旧任务中。所以它现在是一个包含几个子任务的父任务。根据定义,持续时间现在是所有子持续时间的总和。因此,父任务的持续时间无法手动更改,因为它会自动更新。

因此,简单检查旧任务是否包含任何 OutlineChildren 然后跳过它就可以解决我的问题。

If oldTask.OutlineChildren.Count = 0 Then
    oldTask.Duration = lngDuration
End If

谢谢

Found it myself!

The problem was that sometimes tasks would have been added to the old task. So it is now a parent task containing a couple of child tasks. By definition the duration is now the sum of all the child durations. Thus the duration on the parent task can't be changed manually as its updated automatically.

So a simple check if the old tasks contains any OutlineChildren and then skipping it solves my problem.

If oldTask.OutlineChildren.Count = 0 Then
    oldTask.Duration = lngDuration
End If

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文