Is there a way to loop through all of a task's properties

J

Jim

I would like to be able to loop through each property of a task or resource
and print out the property name and value without having to directly name
each one. (Sort of as if it was a collection). Is this possible?

In other words, instead of
debug.print T.ActualFinish
debug.print T.ActualOvertimeCost
debug.print T.ActualStart
etc.

Could I some how just have a loop and iterate through each property without
knowing all their names??
Something like this:

For iTask = 1 To ActiveProject.Tasks.Count
Set T = ActiveProject.Tasks(iTask)
for each prop in T
debug.print T .Prop 'Obviously this does not exist!
next prop
next iTask

My guess is no but i thought I'd ask anyway.

thanks
 
J

Jim

That's exactly what does the trick. I kept looking up properties in VBA
help instead of GetField. thanks
 
J

JackD

If you could be so kind as to post the snippet of your code which traverses
all the properties I'd appreciate it.
I'd like to post it as another example.
 
J

Jim

1) What I wrote is a bit involved so I abstracted what I think is the
essense. It does not iterate through a collection but does satisfy me
needs. The key to doing what I wanted was using the GetField() method with
the IDs provided in the MS Project help file. I copied all the IDs from
Project into MS Word and with a little editing using Word's "Replace " I had
a whole bunch of code that defined every measurement that looked like this:

MyTask(0).Name = "Actual Cost"
MyTask(0).TaskID = pjTaskActualCost
.... etc....
I can post all the measurements (over 100) if you'd like. I did this for
Resources and Tasks objects and skipped those measurements that referred to
other collections, such as calendar.

2) These measurements were defined by my own data type:
Type MSPrjData
Name As String
TaskID As Long
Value As String
End Type
Dim MyTask() as MSPrjData

3) Then I print out my UDT to a file:

For iTask = 1 To ActiveProject.Tasks.Count
Set t = ActiveProject.Tasks(iTask)
If Not t Is Nothing Then
For i = 0 To UBound(MyTask) 'Convert all
measurements to strings
MyTask(i).Value = CStr(t.GetField(MyTask(i).TaskID))
Print #2 "AM", MyTask(i).Name, MyTask(i).Value
Next i
next iTask
Set t = Nothing

I also wrote 2 trivial functions that format dates and boolean the way I
want them. Hopefully I did not abstract my code too much.
 
J

JackD

It looks like you are generating an array with all the task properties, then
iterating through all the elements of that array to get the properties for a
specific task.

I'm a bit confused why you take this approach. Wouldn't it be easier to just
write:

dim t as task
dim ts as tasks
dim propString as String
set ts = activeproject.tasks
for each t in ts
propString = ""
propString = "Foo" & t.name & _
t.ActualCost & _
t.Duration & _
t.ResourceNames & _
...
Print#2 propString
next t
end sub
 
J

Jim

Note for my application which needs to have a dynamic aspect to it; and I
did not want to write a humongous if then else statement.
 
J

JackD

Yet you are still writing a humongous+ amount of code to generate MyTask and
work with it. I don't see how that is any different.
I also don't see how it is any more "dynamic" as the project properties are
always the same.
Maybe there is something I'm missing here.
 

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