Help getting started writing simple Project macro.

C

cr113

I've written lots of code in Excel but I need help getting started in
Project. I need to find the row that contains a certain value in a
given field and make a change to another field in that row.

Suppose I want to find the row where field Text1="abc" and then change
field Name to "123". This piece of code will work unless the value is
not found. I got this from recording a macro.

Find Field:="Text1", test:="equals", Value:="abc", Next:=True,
MatchCase:=False

SelectTaskField Row:=0, Column:="Name"

SetTaskField Field:="Name", Value:="123"

Is there a better way to do this? I'd prefer to specify an object when
writing code. I don't like using methods out of the blue like "Find"
and "SelectTaskField". I don't know what the main objects are in
Project.

For example in Excel I use code like this:

Dim objSheet as Worksheet

set objSheet = Worksheets(1)

objSheet.Cells(1,1) = "abc"


Any help is appreciated! I'm using Project 2007.
 
J

Jan De Messemaeker

Hi,

You're on the right track, Project VBA only rarely uses selections and such.

Try this, it's fast enough:

dim Job as task
for each job in activeproject.tasks
if not job is nothing then
'This to avoid empty lines who generate a nothing object instead of a task
if job.text1="abc" then
job.name="123"
end if
end if
next job

Greetings,

--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620
For availability check:
http://users.online.be/prom-ade/Calendar.pdf
 
C

cr113

Hi,

You're on the right track, Project VBA only rarely uses selections and such.

Try this, it's fast enough:

dim Job as task
for each job in activeproject.tasks
if not job is nothing then
'This to avoid empty lines who generate a nothing object instead of a task
if job.text1="abc" then
job.name="123"
end if
end if
next job

Pefect! That's exactly what I was looking for.

Thanks!

Chuck.
 
J

Jan De Messemaeker

You're welcome, Chuck.

--
Jan De Messemaeker
Microsoft Project Most Valuable Professional
+32 495 300 620
For availability check:
http://users.online.be/prom-ade/Calendar.pdf
Hi,

You're on the right track, Project VBA only rarely uses selections and
such.

Try this, it's fast enough:

dim Job as task
for each job in activeproject.tasks
if not job is nothing then
'This to avoid empty lines who generate a nothing object instead of a task
if job.text1="abc" then
job.name="123"
end if
end if
next job

Pefect! That's exactly what I was looking for.

Thanks!

Chuck.
 

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