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?
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: