Valid object checks...

K

Ken

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.
 
M

Mike Glen

Hi Ken,

Try posting on the developer newsgroup. Please see FAQ Item: 24. Project
Newsgroups. FAQs, companion products and other useful Project information
can be seen at this web address: http://www.mvps.org/project/.

Mike Glen
Project MVP
 
J

JackD

Trap for the error when you do not have a valid selection.
Something like this.

On Error GoTo ErrorHandler ErrorHandler:
msgbox "Nothing has been selected. Please try again" Exit Sub
 
K

Ken

This is very helpful. I appreciate the links.

Here is what I am doing. I want a user to select a task that they want to
run a macro on. I want to make sure that a task is selected. What I have
seen, is if no task is selected (a row, or cell selected that hasn't been
setup) then ActiveSelection = 0. This is easy to take care of. Then I am
trying to make sure that a Task is selected and not something else. This is
where it gets harder. When I use the IsObject(ActiveSelection.Tasks) (I have
actually tried a lot of other calls), I get an error, object required '424'.
I am a c programming, and I thought all I had to do was check to see that the
object existed before I go further. If the object doesn't exist, send the
user a message, otherwise continue processing the code. It looks like
trapping the error is a way to make sure that the object exists. I was
hoping there was a way of say if object is valid, continue on.

Thank you for all of your time. I think I am going to do a little reading,
so I can understand more of what is happening.
 
J

John

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
 
J

John

Ken said:
This is very helpful. I appreciate the links.

Here is what I am doing. I want a user to select a task that they want to
run a macro on. I want to make sure that a task is selected. What I have
seen, is if no task is selected (a row, or cell selected that hasn't been
setup) then ActiveSelection = 0. This is easy to take care of. Then I am
trying to make sure that a Task is selected and not something else. This is
where it gets harder. When I use the IsObject(ActiveSelection.Tasks) (I have
actually tried a lot of other calls), I get an error, object required '424'.
I am a c programming, and I thought all I had to do was check to see that the
object existed before I go further. If the object doesn't exist, send the
user a message, otherwise continue processing the code. It looks like
trapping the error is a way to make sure that the object exists. I was
hoping there was a way of say if object is valid, continue on.

Thank you for all of your time. I think I am going to do a little reading,
so I can understand more of what is happening.
Ken,
Something sounds a little off base. If the user selected a task, how can
it be blank? Yes, I guess they could inadvertently select a blank line
but then why would they go ahead and run the macro?

Depending on what the macro is designed to do, I can envision the user
selecting a task from a task type view (e.g. Gantt Chart) or an
assignment in the Resource Usage view or perhaps a task in the Task
Usage view. These are all valid task selections and can be handled in
the code. The user will never select a task in a pure resource type of
view, such as the Resource Sheet, because there are no tasks in a pure
resource view?

Although I understand that users can sometimes do some "unconventional"
things and therefore a good set of controlled error traps in code is
well advised, but from what you have explained, the need for error traps
isn't all that apparent.

John
Project MVP
 

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