将 Task.Duration 分配给 VBA Microsoft Project 中已添加的任务
我目前正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到的!
问题是有时任务会被添加到旧任务中。所以它现在是一个包含几个子任务的父任务。根据定义,持续时间现在是所有子持续时间的总和。因此,父任务的持续时间无法手动更改,因为它会自动更新。
因此,简单检查旧任务是否包含任何 OutlineChildren 然后跳过它就可以解决我的问题。
谢谢
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.
Thanks