Reference Task Textn property using a loop

J

jruss

I'm writing a macro for a manager who needs to store values for a
resource in the task object. She wants to have the value for Resource1
go into Text1, Resource2 in Text2, etc.

Is there any way to reference the Textn properties in a loop rather
than have a massive switch that checks the current index and assigns it
to a specific Textn field?

There are no examples that I can find that do this, and I don't know if
it's even possible.

Thanks in advance.
 
J

JackD

Use the FieldNameToFieldConstant method. I have an example of this on my
website.

Look for the CheckFields for Proj2002+ macro about 2/3 of the way down the
page (in the formatting and automating section)

http://masamiki.com/project/macros.htm

the key part is using a string and a variable (i) which is incremented

for i = 1 to 30
msgbox Activeproject.tasks(1).GetField(FieldNameToFieldConstant("Task" & i,
pjtask))
next i

Your problem is that not every task may have just one resource. What do you
want to do in that case?

Your code will have three nested control structures

for each task in activeproject.tasks
for each resource in task.resources
for i = 1 to 30
....
 
J

John

I'm writing a macro for a manager who needs to store values for a
resource in the task object. She wants to have the value for Resource1
go into Text1, Resource2 in Text2, etc.

Is there any way to reference the Textn properties in a loop rather
than have a massive switch that checks the current index and assigns it
to a specific Textn field?

There are no examples that I can find that do this, and I don't know if
it's even possible.

Thanks in advance.

jruss,
Since the field ID in Project is an application unique Long data type, a
simple loop employing a counter cannot be used. There are basically two
choices. The first and most straightforward, although it may seem crude,
is to set up a look up table that provides the appropriate Textn long
value at each query. The table wouldn't be all that "massive" since
there are only 30 Textn fields and if the file has less than 30 total
resources, all 30 Textn fields do not need to be represented. Use the
resource ID to index the look up table.

A second but more ambitious approach that is amenable to a loop
structure is to use the numeric equivalent of the pjField constant.
Unfortunately the numeric sequence is not in nice consecutive order but
it does have some structure. Take a look at the Textn values you need
and then look up their numeric long value. As I recall, except for a
couple of discontinuities, an incrementing counter can be used to cycle
through the desired group. The discontinuities can be handled with
simple "If" statements.

Hope this helps.
John
Project MVP
 
J

JackD

Jack,
Just to clarify, the poster didn't specify which version Project he is
using. The FieldNameToFieldConstant Method may not be available (e.g.
the method is not available for Project 2000).

I wondered about the multiple resources per task also but then I just
assumed for a given task that Resource1 data (i.e. assignment 1) would
write to Text1, Resource2 data to Text2 and etc.

John

True about the project 2002+ requirement.

-Jack
 
J

John

JackD said:
Use the FieldNameToFieldConstant method. I have an example of this on my
website.

Look for the CheckFields for Proj2002+ macro about 2/3 of the way down the
page (in the formatting and automating section)

http://masamiki.com/project/macros.htm

the key part is using a string and a variable (i) which is incremented

for i = 1 to 30
msgbox Activeproject.tasks(1).GetField(FieldNameToFieldConstant("Task" & i,
pjtask))
next i

Your problem is that not every task may have just one resource. What do you
want to do in that case?

Your code will have three nested control structures

for each task in activeproject.tasks
for each resource in task.resources
for i = 1 to 30
...

Jack,
Just to clarify, the poster didn't specify which version Project he is
using. The FieldNameToFieldConstant Method may not be available (e.g.
the method is not available for Project 2000).

I wondered about the multiple resources per task also but then I just
assumed for a given task that Resource1 data (i.e. assignment 1) would
write to Text1, Resource2 data to Text2 and etc.

John
 

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