Project VBA to trap an empty task

P

Pete Rooney

I'm trying to write some code to scan all my task names and expand outlines
if a task name matches a condition. This is fine until the scan hits a blank
task at which point I get an "Object Variable or with block variable not set"
message pointing to the If TaskName.name="Software Management" line

Please excuse the rough code - I've only been VBAing in Project for three
days.

Can anyone advise as to what I should do - if I take out all the blank tasks
in the project, the error doesn't occur, but I want to know why - I probabbly
need to get out more, too.

I also have a hideous feeling that I'm on the wrong board - I'm sure the one
I used on my colleaguie's PC was orange at the top of the screen, but I can't
find out where I was!

Cheers

Pete Rooney


Sub ASMFilter()
Dim TaskName As Task
Dim I As Integer
SelectTaskColumn Column:="Name"
OutlineShowTasks OutlineNumber:=pjTaskOutlineShowLevel9
OutlineShowTasks OutlineNumber:=pjTaskOutlineShowLevel3
For Each TaskName In ActiveSelection.Tasks
If TaskName.Name = "Software Management" Then
'MsgBox (TaskName & " " & TaskName.Name)
TaskName.OutlineShowSubTasks
End If
Next
End Sub
 
J

JackD

John,

Using TaskName as a variable is close to being confusing.

For each Task in ActiveSelection.Tasks
if not Task is nothing then
'do stuff

would be better.
Or at least include

dim TaskName as Task
in the code. I typically use:

dim t as task
for each t in activeproject.tasks
if not t is nothing then
'do stuff
end if
next t
 
J

John

Pete,
First of all this is exactly the right newsgroup for your question. It's
amazing how many queries we get in this and other related Project
newsgroups that have nothing to do with Project. Also, I think "Orange
PC" is a company that used to make a "hard PC" card for MacIntosh
computers:)

All you need to ferret out blank lines is one simple sequence of code as
follows:

Sub ASMFFilter()
..
..
For Each TaskName in ActiveSelection.Tasks
If not TaskName is Nothing Then
[your code here]
End If
Next
End Sub

The "If not TaskName is Nothing Then" line detects the null object
presented by a blank line and jumps around it. I never did like this
negative logic construct but it is effective and everybody I know
routinely puts it in their VBA code when looping through tasks (or
resources) in a Project file. I personally don't like blank lines in a
file. In my opinion, they add no value but I still include the null
detection code when I write macros for others, because not everybody
shares my opinion.

Hope this helps.
John
Project MVP
 
P

Peter Rooney

John,

This is EXACTLY what I needed - thank you very much!
Do you guys work for Microsoft answering things like this, or do you just
dip in out or the boards ourt of the goodness of your hearts - I wiah I was
that confident in my abilities!

Anyway, thanks again.

Pete
 
P

Peter Rooney

Jack,

Thanks for the useful pointer - it was ME who came up with the dodgy
variable name, not John. The suggestions worked fine - thank you for your
help.
Do you work for Microsoft, or do you just do this sort of stuff because you
want to?

Cheers

Pete Rooney

JackD said:
John,

Using TaskName as a variable is close to being confusing.

For each Task in ActiveSelection.Tasks
if not Task is nothing then
'do stuff

would be better.
Or at least include

dim TaskName as Task
in the code. I typically use:

dim t as task
for each t in activeproject.tasks
if not t is nothing then
'do stuff
end if
next t

--
-Jack ... For project information and macro examples visit
http://masamiki.com/project

..
John said:
Pete,
First of all this is exactly the right newsgroup for your question. It's
amazing how many queries we get in this and other related Project
newsgroups that have nothing to do with Project. Also, I think "Orange
PC" is a company that used to make a "hard PC" card for MacIntosh
computers:)

All you need to ferret out blank lines is one simple sequence of code as
follows:

Sub ASMFFilter()
.
.
For Each TaskName in ActiveSelection.Tasks
If not TaskName is Nothing Then
[your code here]
End If
Next
End Sub

The "If not TaskName is Nothing Then" line detects the null object
presented by a blank line and jumps around it. I never did like this
negative logic construct but it is effective and everybody I know
routinely puts it in their VBA code when looping through tasks (or
resources) in a Project file. I personally don't like blank lines in a
file. In my opinion, they add no value but I still include the null
detection code when I write macros for others, because not everybody
shares my opinion.

Hope this helps.
John
Project MVP
 
M

Mike Glen

Hi Pete,

All MVPs are volunteers giving their time and expertise for nothing,
although MS does give an annual award as a "thank you". We are definitely
not MS employees and being independent, we can and do say bad things about
their software, if it is deserved, as well as good! If you want further
info, see here:
http://mvp.support.microsoft.com/default.aspx?scid=fh;en-gb;mvpprogover


Mike Glen
Project MVP



Peter said:
John,

This is EXACTLY what I needed - thank you very much!
Do you guys work for Microsoft answering things like this, or do you
just dip in out or the boards ourt of the goodness of your hearts - I
wiah I was that confident in my abilities!

Anyway, thanks again.

Pete





John said:
Pete,
First of all this is exactly the right newsgroup for your question.
It's amazing how many queries we get in this and other related
Project newsgroups that have nothing to do with Project. Also, I
think "Orange PC" is a company that used to make a "hard PC" card
for MacIntosh computers:)

All you need to ferret out blank lines is one simple sequence of
code as follows:

Sub ASMFFilter()
..
..
For Each TaskName in ActiveSelection.Tasks
If not TaskName is Nothing Then
[your code here]
End If
Next
End Sub

The "If not TaskName is Nothing Then" line detects the null object
presented by a blank line and jumps around it. I never did like this
negative logic construct but it is effective and everybody I know
routinely puts it in their VBA code when looping through tasks (or
resources) in a Project file. I personally don't like blank lines in
a file. In my opinion, they add no value but I still include the null
detection code when I write macros for others, because not everybody
shares my opinion.

Hope this helps.
John
Project MVP
 
P

Peter Rooney

Thanks for clearing that up, Mike - maybe I'll sort a problem out for someone
one day, too!

Cheers

Pete


Mike Glen said:
Hi Pete,

All MVPs are volunteers giving their time and expertise for nothing,
although MS does give an annual award as a "thank you". We are definitely
not MS employees and being independent, we can and do say bad things about
their software, if it is deserved, as well as good! If you want further
info, see here:
http://mvp.support.microsoft.com/default.aspx?scid=fh;en-gb;mvpprogover


Mike Glen
Project MVP



Peter said:
John,

This is EXACTLY what I needed - thank you very much!
Do you guys work for Microsoft answering things like this, or do you
just dip in out or the boards ourt of the goodness of your hearts - I
wiah I was that confident in my abilities!

Anyway, thanks again.

Pete





John said:
Pete,
First of all this is exactly the right newsgroup for your question.
It's amazing how many queries we get in this and other related
Project newsgroups that have nothing to do with Project. Also, I
think "Orange PC" is a company that used to make a "hard PC" card
for MacIntosh computers:)

All you need to ferret out blank lines is one simple sequence of
code as follows:

Sub ASMFFilter()
..
..
For Each TaskName in ActiveSelection.Tasks
If not TaskName is Nothing Then
[your code here]
End If
Next
End Sub

The "If not TaskName is Nothing Then" line detects the null object
presented by a blank line and jumps around it. I never did like this
negative logic construct but it is effective and everybody I know
routinely puts it in their VBA code when looping through tasks (or
resources) in a Project file. I personally don't like blank lines in
a file. In my opinion, they add no value but I still include the null
detection code when I write macros for others, because not everybody
shares my opinion.

Hope this helps.
John
Project MVP
 
M

Mike Glen

Yes - go for it! People need all the help they can get and new ideas or ways
of achieving an end are always welcome. Good luck :)


Mike Glen
Project MVP

Peter said:
Thanks for clearing that up, Mike - maybe I'll sort a problem out for
someone one day, too!

Cheers

Pete


Mike Glen said:
Hi Pete,

All MVPs are volunteers giving their time and expertise for nothing,
although MS does give an annual award as a "thank you". We are
definitely not MS employees and being independent, we can and do say
bad things about their software, if it is deserved, as well as good!
If you want further info, see here:
http://mvp.support.microsoft.com/default.aspx?scid=fh;en-gb;mvpprogover


Mike Glen
Project MVP



Peter said:
John,

This is EXACTLY what I needed - thank you very much!
Do you guys work for Microsoft answering things like this, or do you
just dip in out or the boards ourt of the goodness of your hearts -
I wiah I was that confident in my abilities!

Anyway, thanks again.

Pete





:


Pete,
First of all this is exactly the right newsgroup for your question.
It's amazing how many queries we get in this and other related
Project newsgroups that have nothing to do with Project. Also, I
think "Orange PC" is a company that used to make a "hard PC" card
for MacIntosh computers:)

All you need to ferret out blank lines is one simple sequence of
code as follows:

Sub ASMFFilter()
..
..
For Each TaskName in ActiveSelection.Tasks
If not TaskName is Nothing Then
[your code here]
End If
Next
End Sub

The "If not TaskName is Nothing Then" line detects the null object
presented by a blank line and jumps around it. I never did like
this negative logic construct but it is effective and everybody I
know routinely puts it in their VBA code when looping through
tasks (or resources) in a Project file. I personally don't like
blank lines in a file. In my opinion, they add no value but I
still include the null detection code when I write macros for
others, because not everybody shares my opinion.

Hope this helps.
John
Project MVP
 
J

JackD

Just for fun. I originally started because I couldn't stand to see people
suffering from the same problems that I had just gone through, but now I
find it stretches my mind kind of like working crossword puzzles .

It is rare to see someone who works at microsoft posting here, but they do
on occasion.
 

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