Incorrect Task "Count"

M

MWE

I am running MSProject2000 under WinXP. I recently ran into a problem with
the "Count" value in an MSProject VBA application. The value for
Activeproject.Tasks.Count is NOT the number of tasks. Rather it is the
highest ID number in the current collection of tasks. Thus if you have real
tasks in rows 1 through 10, and 12 through 20, and use row 11 as a separator
row with no data in it, Activeproject.Tasks.Count = 20 but there is no task
#11. If your code sequences through the 20 tasks, execution stops with an
"Object Variable Not Set" error. You have the same problem whether you use
a traditiional index, for example:

Dim I as Integer
For I = 1 to ActiveProject.Tasks.Count
... some code relating to Tasks(I) ...
Next I

or the For Each approach, for example:

Dim ThisTask As Task
For Each ThisTask In ActiveProject.Tasks
... some code relating to ThisTask ...
Next ThisTask

I am sure that others have encountered this problem. Using error
trapping code to test for the existence a task works, but is a bandaid. Is
there a better way to approach this problem?

Thanks
 
J

John

MWE said:
I am running MSProject2000 under WinXP. I recently ran into a problem with
the "Count" value in an MSProject VBA application. The value for
Activeproject.Tasks.Count is NOT the number of tasks. Rather it is the
highest ID number in the current collection of tasks. Thus if you have real
tasks in rows 1 through 10, and 12 through 20, and use row 11 as a separator
row with no data in it, Activeproject.Tasks.Count = 20 but there is no task
#11. If your code sequences through the 20 tasks, execution stops with an
"Object Variable Not Set" error. You have the same problem whether you use
a traditiional index, for example:

Dim I as Integer
For I = 1 to ActiveProject.Tasks.Count
... some code relating to Tasks(I) ...
Next I

or the For Each approach, for example:

Dim ThisTask As Task
For Each ThisTask In ActiveProject.Tasks
... some code relating to ThisTask ...
Next ThisTask

I am sure that others have encountered this problem. Using error
trapping code to test for the existence a task works, but is a bandaid. Is
there a better way to approach this problem?

Thanks

MWE,
The reason the Count property doesn't work is because indeed the blank
line is a "task" (check the UID) or should I say, space has been
allocated for a task. Unfortunately, the "task" has no object properties
(e.g. name, dates, etc.) and therefore the "object variable not set"
error occurs.

The most common method for dealing with plans that have blanks
separating groups of tasks is the following VBA structure:
For Each t in ActiveProject.Tasks
If Not t is Nothing Then
... your code here
End If
Next t

I never have been a fan of the negative logic statement but it is a
tried and true method that works. This type of structure will also work
if there are blank lines on the Resource Sheet. Note, if the code
operates on a filtered set of tasks, the "If Not ..." statement is not
needed because there will be no blanks in the filtered group. However
you can't simply use the same "For Each ..." statement on a filtered
set. Rather you need to set an object on the filtered group similar to
the following:
SelectTaskColumn
Set area = ActiveSelection.Tasks
For Each t in area

Hope this helps.
John
Project MVP
 
M

MWE

John: thanks for your prompt and useful reply. I had suspected that
MSProject set aside the "space" for a task. The "If Not t is Nothing then
...." is an interesting approach. I agree that the negative logic feels
wrong, but it seems to work.

By-the-way, where can I find a good reference for the objects used in
MSProject. I am particularly interested in those that are "available" for
user-defined activities and not explicitly used by MSProject for core
processes. I assume that Task.FlagX and Task.TextX are available, but have
not been able to confirm that.

Thanks again

MWE
 
J

JackD

From the help menu select reference and look at the information on fields.
All the numbered fields

Text1-30
Cost 1-10
Number1-20
Duration1-10
Date1-10
Flag1-20
Start1-10
Finish1-10
Outline Code1-10

are what project calls "custom fields"

So are things like Contact.
However, using VBA you can change the value of just about any non-calculated
field.
 
E

Earl Lewis

MWE,

The second part of this last post is a little confusing. The entire MSProject object model is available to you in the VBA environment, i.e. you can read and many times, write to almost any object property, based on how that propery is defined in the object model.

What you seem to be asking is: what are the fields within the object model and which of those can you use. True? Directly from the built-in help:

Customize Fields dialog box, Custom Fields tab

Use the Custom Fields tab on the Customize Fields dialog box to set custom attributes for custom fields. Custom fields include Cost1 through Cost10, Date1 through Date10, Duration1 through Duration10, Finish1 through Finish10, Flag1 through Flag20, Number1 through Number20, Start1 through Start10, and Text1 through Text30.

And about the blank rows affecting the count - don't use blank rows if at all possible. I know people like their 'white space' but it really is a bad practice that should be avoided.

Hope that helps.

Earl
John: thanks for your prompt and useful reply. I had suspected that
MSProject set aside the "space" for a task. The "If Not t is Nothing then
...." is an interesting approach. I agree that the negative logic feels
wrong, but it seems to work.

By-the-way, where can I find a good reference for the objects used in
MSProject. I am particularly interested in those that are "available" for
user-defined activities and not explicitly used by MSProject for core
processes. I assume that Task.FlagX and Task.TextX are available, but have
not been able to confirm that.

Thanks again

MWE
 
J

JackD

Earl Lewis said:
MWE,

The second part of this last post is a little confusing. The entire
MSProject object model is available to you in the VBA environment, i.e. you
can read and many times, write to almost any object property, based on how
that propery is defined in the object model.
What you seem to be asking is: what are the fields within the object model
and which of those can you use. True? Directly from the built-in help:
Customize Fields dialog box, Custom Fields tab

Use the Custom Fields tab on the Customize Fields dialog box to set custom
attributes for custom fields. Custom fields include Cost1 through Cost10,
Date1 through Date10, Duration1 through Duration10, Finish1 through
Finish10, Flag1 through Flag20, Number1 through Number20, Start1 through
Start10, and Text1 through Text30.
And about the blank rows affecting the count - don't use blank rows if at
all possible. I know people like their 'white space' but it really is a bad
practice that should be avoided.
Hope that helps.

Earl

I have to second Earl here on the use of blank lines. Even though it is
simple to filter them out, it is best if you just avoid it in the first
place.

-Jack
 
J

Jan De Messemaeker

In summary, so that you don't have to make a list of ciustom fields:

"If the name ends with a numeric, it's all yours"
HTH
 

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