SelectTaskField confusion when outline partly closed

D

djlewis2

I record a macro (with "Absolute (ID)" row references and select the
name field of task with ID=45, and get this recorder.

Sub Macro5()
SelectTaskField Row:=45, Column:="Name", RowRelative:=False
End Sub

OK, seems good. But now I run that sub and look at the project gantt
chart, and I see that the task with ID 101 is selected. That's the
45th task counting only visible tasks from the top, not counting tasks
hidden by closed outline nodes.

What is going on? Apart from the macro recorder appearing to lie, how
the heck -- in VBA -- do I go to a task with a specific ID (or UniqueID
for that matter).

BTW, with Relative Row Reference, it seems to work properly. The
offset recorded is the number of rows actually showing in the outline,
and replaying the generated macro does the same thing.

Thanks. --David.
 
R

Rod Gill

Hi,

The key thing with Project VBA (and Excel VBA) is NOT to move the cursor and
go to Tasks or cells unless absolutely necessary. So, if you want to read
the Duration and contents of Text1 for the task in Row 45 use:

Sub Test
Dim Tsk as Task
set Tsk=ActiveProject.Tasks(45)
msgbox "Task: " & Tsk.Name & vbcrlf _
"Duration="& Tsk.Duration/60/8 & "d" & vbcrlf _
'Text1="& Tsk.Text1
End Sub

No need to move the cursor at all. For Unique ID use:

set Tsk=ActiveProject.Tasks.UniqueID(45)
 
D

djlewis2

Great... thanks. I take it the problem with relative SelectTaskField is
a bug in Project, or at least in the macro recorder. Makes no sense
otherwise. But I will definitely stay away from it and its cousins.

--David
 

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