Ken said:
I am new to VB, and thought I could do something like this:
if ActiveSelection > 0 and isObject(ActiveSelection.Tasks) then
:
if ActiveSelection.Tasks.Count > 10 then
message("Can't work on more than 10 tasks at a time")
end if
:
end if
This doesn't work if you have Resources selected or if you have no tasks
selected. Can anybody give suggestions on how to resolve this?
Thanks.
Ken,
It doesn't work with resources because you are dealing with a Task
objects. It also doesn't work with no tasks selected because "null" is
not a valid object.
It isn't clear what you are trying to do with the first "If" statement
but let me give you a little background. When working with VBA there are
two basic modes - foreground processing and background processing. When
you record a macro the code is generally structured with various things
(e.g. task data, resource data, etc.) actively selected. This code
operates in foreground processing. With foreground processing, as the
code runs, elements on the screen are actively selected. This tends to
be less flexible, visually pronounced, and slower. With background
processing, project objects are accessed directly and as the code runs
the user may not see any visual action at all, but the code is generally
more flexible (more things can be done) and the code runs faster. The
following code snippet is typical for looping through all tasks in a
plan. It doesn't matter what view is active because the code runs
independent of the view.
Sub DoAllTasks()
' cycle through all elements of the Tasks collection
For Each t in ActiveProject.Tasks
' jump around null tasks, for example, blank lines in the plan
If Not t is Nothing Then
' one or more lines of code to do something,
' in this case put the task name in the spare Text1 field
t.Text1 = t.Name
End If
Next t
End Sub
In some cases you may only want to operate on a select set of tasks.
There are various ways to do this. One of the methods I use is to first
filter for the select set of tasks and then set up the filter set as a
separate object. The filter can either be applied manually before the
code is run or it can be incorporated into the code itself. Assume a
filter is applied manually. This code will do the same thing as the
above code except it will only be done to the selected tasks.
Sub DoSelectedTasks()
SelectTaskColumn
Set area = ActiveSelection.Tasks
For each t in area
t.Text1 = t.Name
Next t
End Sub
The above code operates in a combination of foreground and background
processing. You will note that the "If Not t is Nothing Then" statement
is not needed because there will be no blank lines. However if the
filter was applied by code, the result might be no tasks and some type
of error trapping would be necessary or a runtime error will occur.
If you are just starting out with VBA you might want to check out these
sites for some useful reference material. First go to the MVP website at:
http://www.mvps.org/project/links.htm
and find the link at the bottom of the page for, "Project 98 Visual
Basic Environment Training Materials". Even though it is for Project 98,
it is equally applicable to all current versions of Project. The only
real difference is that newer versions of Project will have an expanded
Project object library. For a nice collection of VBA code examples, go
to Jack Dahlgren's website at:
http://masamiki.com/project/macros.htm
Hope this helps.
John
Project MVP