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!
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