The First in a Series of Short Notes About Using Project VBA
The tasks collection is simply a collection of all the tasks in a project. It is the starting point for most Visual Basic programming exercises so it is important to know how to use it. The first thing is in how to set it. This is done using the Set
keyword.
Typically one sets a collection to all the tasks in the project, but it is possible to set it to some other task collection.
Here is an example of setting it to the tasks in the active project:
Dim ts as Tasks
Set ts = ActiveProject.Tasks
Another useful trick is to filter the project first and then set the task collection to the set of filtered tasks:
SelectAll
Set ts = ActiveSelection.Tasks
Once we have the task collection we can go through it in a number of ways. If we want a specific task we can ask for it by index. For example if we want the first task the code would be:
Dim t as task
Set t = ts(1)
Quite often we want to do something to all tasks in the project. In that case we would set the task collection as above and then loop through it using a for..next structure:
For Each t in ts
t.Text5 = "Foo"
Next t
This approach works until you hit a blank line in the project. In the case of the blank line the task is what Project refers to as "Nothing". You can do nothing with Nothing, so setting the Text5 value for Nothing will give you an error. Luckily you can check to see if a task is Nothing and therefore skip doing anything that would cause an error and stop your code. To do this we add a simple If statement:
For Each t in ts
If not t is Nothing then
t.Text5 = "Foo"
End If
Next t
We can do a similar thing to ignore summary tasks. You might want to do this when altering a value like duration which is not something that you can edit directly for a summary task. I use something like this:
If not t.Summary Then
'do stuff
End If
Putting it all together we have this generic structure to loop through all tasks in a project:
Dim ts as Tasks
Dim t as Task
Set ts = ActiveProject.Tasks
For Each t in ts
If Not t is Nothing Then
If Not t.Summary Then
'do something
End If
End If
Next t
By putting your code in the middle of this structure (where it says "do something" you can be sure it will be applied to all the regular tasks in the project and won't generate an error when it hits a blank line.
Comments (5)
This keeps giving me a 424 error, i'm using 2003 is there anything else I need to consider
Posted by Simon | October 8, 2005 12:31 PM
Posted on October 8, 2005 12:31
Simon,
Hard to say why you are getting the error because you haven't mentioned which of these snippets you are using. It may be that you are missing the start and end.
This for example is a valid subprocedure:
sub foo()
dim t as task
dim ts as tasks
set ts = activeproject.tasks
for each t in ts
if not t is nothing then
if not t.summary then
t.text5 = "foo"
end if
end if
next t
end sub
If you don't include the "sub" and "end sub" then nothing will happen. If you want me to debug further, please post the actual code you are using. It is difficult to figure out where it might have gone wrong without seeing it.
Posted by Jack | October 8, 2005 5:08 PM
Posted on October 8, 2005 17:08
I learn something new every day from your website. Jack, thank you for the information! Regards, Denys
Posted by Denys Lebedev | October 31, 2008 8:09 AM
Posted on October 31, 2008 08:09
I need a macro that selects tasks and sets the "Free Slack" amount to a value which I set. I've been using a flag field and if "yes" then set jTask.FreeSlack = " 30 d" - the macro is hung up on the jTask.Flag5 = "Yes" I can't get pass this line of code - help! thanks - Jay.
------------
FreeSlack is calculated based on your task durations and dependencies. Unfortunately you can not set it manually. -JD
Posted by Jay | November 8, 2008 9:37 AM
Posted on November 8, 2008 09:37
Thank you! After poring over countless VBA examples and compiling snippets of code, I found this example and it worked perfectly. Thanks again!
Posted by VBARookE | December 9, 2009 8:16 AM
Posted on December 9, 2009 08:16