Add Child Task: a tasks collection suprise

P

phreed

Here is an interesting problem/bug related to VBA and MS Project.
As near as I can tell there is no simple function to add a child task.

I would have thought the following would do what I wanted/expected.

Set ChildTask = ParentTask.OutlineChildren.Add(Name:=Name)

However, it appears that the Add method for any Tasks Collection always
acts as if the object were "ActiveProject.Tasks"!
I can easily make a function that is close to what I want,
except it adds the new task as first child rather than a last child.

Private Function AddChildTask(ByRef Task As Task, Optional ByRef Name
As String) As Task
Set AddChildTask = Task.OutlineChildren.Add(Name:=Name,
Before:=(Task.ID + 1))
AddChildTask.OutlineLevel = Task.OutlineLevel + 1
End Function

I haven't come up with a simple function to add the new task as a last
child.
 
J

Jan De Messemaeker

Hi,

Not that simple, but once you get the ID of the next summary task you can
use .add with Before.
HTH
 
P

phreed

So a recursive function or a loop.
I guess the following isn't too bad.

Private Function AddChildTask(ByRef Task As Task, Optional ByRef Name
As String) As Task
Dim TempTasks As Tasks
Set TempTasks = Tasks.OutlineChildren
Dim LastTask As Task
Set LastTask = Task
Do While TempTasks.Count > 0
Set LastTask = TempTasks(TempTasks.Count)
Set TempTasks = LastTask.OutlineChildren
Loop
Set AddChildTask = Task.OutlineChildren.Add(Name:=Name,
Before:=(LastTask.ID + 1))
AddChildTask.OutlineLevel = Task.OutlineLevel + 1
End Function
 
J

Jan De Messemaeker

Hi,

Outlinechildren is a property of a TASK, not of TASKS I believe.
HTH
 
P

phreed

Yes, especially given that there is no 'Tasks' object defined ;-)
Other than that typo it seems to work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top