Share via

MS Project VBA Assignment Add Method

Anonymous
2013-09-05T22:04:44+00:00

Hi,

I want to add assignments to a task via a macro.

I found this

http://msdn.microsoft.com/en-us/library/office/ff864822.aspx

ActiveProject.Tasks(1).Assignments.Add ResourceID:=212

This works, but I also want to state the Units.

I tried this:

ActiveProject.Tasks(1).Assignments.Add (ResourceID:=9, Units:=0.6)

But it did not work...

But according to the structure (expression .Add(TaskID, ResourceID, Units))

I thought this should be right.

What am I doing wrong?

I simply want to add an assignment by stating TaskID, ResourceID and Units.

How can I do that?

Sometimes I get a runtime error 1001 saying that there is a problem with this assignment.

Kind regards,

Nathalie
Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. John Project 49,710 Reputation points Volunteer Moderator
    2013-09-06T02:43:42+00:00

    Nathalie,

    There is nothing inherently wrong with your syntax

    ActiveProject.Tasks(1).Assignments.Add ResourceID:=9, Units:=0.6

    However, if you attempt to add that same resource as another assignment to the same task, you will get a runtime error, because that resource is already assigned to the task.

    I'm just guessing here but what I think you may be trying to do is to change the resource assignment, not add the assignment. If you want to change an existing assignment, you first must delete the assignment and then use the add method. Or, if you just want to change the units assignment of an existing assignment, the following syntax will work.

    ActiveProject.Tasks(1).Assignments(1).Units=0.6

    Note: you would need a loop of all that tasks assignments to determine which assignment index to update.

    Hope this helps.

    John

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. John Project 49,710 Reputation points Volunteer Moderator
    2013-09-06T15:15:57+00:00

    Rod,

    I kind of assumed his statement was to the right of an "equals" sign, otherwise he would have gotten a compile error instead of a runtime error.

    But, you point is well taken.

    John

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-09-06T05:10:56+00:00

    The other possibility is you got the syntax wrong. In VBA you only surround parameters with brackets when a value is to be returned. Try:

    ActiveProject.Tasks(1).Assignments.Add ResourceID:=9, Units:=0.6

    Which is the same as what you had, but no brackets. John actually did the same as I just did, be he forgot to mention it!

    Was this answer helpful?

    0 comments No comments