How to I program a macro to run each time Project is opened?

S

Sarah23

I created a macro that highlights overdue tasks in red. I would like to
program this macro to run each time Project is opened and each time a change
is made to any project. Is there a way to do this? Thanks so much for your
help!

Sarah
 
J

Jan De Messemaeker

Hi Sarah,

Alt+F11 opens the VB Editor
On the Project Explorer (left)
Under VBAProject(name of the project here) find ThisProject(Nameofthe
project here) and double-click
In the ThisProject(Code) window that opens, left instead of (General) select
Project
A Private Sub is prepared for you.
Fill in the VBA code there; it will fire at each open of this file

If you do the same under ProjectGlobal(Global.mpt) that sub will fire any
time "A" project is opened.


Hope this helps
 
J

John

Sarah23 said:
I created a macro that highlights overdue tasks in red. I would like to
program this macro to run each time Project is opened and each time a change
is made to any project. Is there a way to do this? Thanks so much for your
help!

Sarah

Sarah,
One of my fellow MVPs, Jack Dahlgren, has a nice website with macro
examples including some on running macro based on Events such as when a
file is opened. To access Jack's site, go to:
http://masamiki.com/project/macros.htm

Hope this helps.
John
 
S

Sarah23

I'm not sure if I am just doing something wrong or what, but my macro still
isn't running when I open Project. It works fine when I use the shortcut I
added for it. Am I entering everything correctly? In the code window, I'm
selecting Project in the left menu and Open is selected automatically on the
right. Then in blank line between Private Sub and End Sub I'm typing the
name of my macro (in this case, OverdueTasks). Like so:

Private Sub Project_Open(By Val pj As Project)
OverdueTasks
End Sub

Is this correct? Do I need anything before closing the code window?
Because right now I'm saving, then closing Project and reopening it, and the
macro isn't running. I tested this same method in Excel and that worked
perfectly...so I'm not sure what to do. Sorry, I'm a total macro newbie.
Thanks for your help both of you!
 
J

Jan De Messemaeker

Hi Sarah

Every word counts
This macro will fire when you open THE project, not Project.
HTH
 
S

Sarah

Sorry, I wasn't clear. Although I do want this macro to run each time I open
Project, for now I am testing one particular project, in which I saved the
macro using the format I listed yesterday. When I reopen this particular
project, the macro does not run. Sorry, I hate to keep bothering anyone, but
I really do appreciate the help!
 
J

John

Sarah23 said:
I'm not sure if I am just doing something wrong or what, but my macro still
isn't running when I open Project. It works fine when I use the shortcut I
added for it. Am I entering everything correctly? In the code window, I'm
selecting Project in the left menu and Open is selected automatically on the
right. Then in blank line between Private Sub and End Sub I'm typing the
name of my macro (in this case, OverdueTasks). Like so:

Private Sub Project_Open(By Val pj As Project)
OverdueTasks
End Sub

Is this correct? Do I need anything before closing the code window?
Because right now I'm saving, then closing Project and reopening it, and the
macro isn't running. I tested this same method in Excel and that worked
perfectly...so I'm not sure what to do. Sorry, I'm a total macro newbie.
Thanks for your help both of you!

Sarah,
I haven't done any Event macro in quite a while so I had to do a little
testing. The macro name needs to be a string and needs to be "called".
Here is the code that worked for me.
Private Sub Project_Open(By Val pj As Project)
Macro "OverdueTasks"
End Sub

If the Open code is attached to "ThisProject(your file)", you will need
to save the project and then close it. You do not need to quit Project.
If the Open code is attached to the Global (i.e. ThisProject(global.mpt)
then it will run whenever any project file is opened as Jan mentioned in
his reply.

Also, if the Open code is attached to a specific file you will probably
get a warning message to enable or disable macros when you open the
file. You can either put up with the message each time or you can set
your macro security level to "low" (the default is "medium"). If you do
not open strange Project files (i.e. may have evil macros), the low
setting is probably ok. To change the security setting, from Project go
to Tools/Macro/Security. Note: this is a Project level setting (i.e. it
applies to all Project files).

Hope this helps.
John
 
S

Sarah

It still doesn't work! I typed in the macro exactly as suggested, saved the
file, reopened it, and nothing. If there are any other suggestions, I would
appreciate them...but maybe this is a lost cause? Thank you so much for your
help though. There must be something wrong on my end but I can't figure out
what it is.
 
J

JackD

Sarah,


As a first step I suggest taking the code in your overdue tasks macro and
putting it in the Project_open macro.

Private Sub Project_Open(By Val pj As Project)
put the body of your macro here (without the Sub and End Sub lines)
End Sub

Where is your overduetasks macro stored?
Can you post the code for it?

You might also want to take a look at my instructions for project open on my
website. I just updated it today.

http://masamiki.com/project/project_open.htm
 
M

Marty

Hi Sarah,
I need to identify overdue tasks as well. Would you be willing to share
your macro code? I would be more than willing to help with your project open
issue in return.
 
M

Mike Glen

Hi Marty,

Try posting on the developer newsgroup as this one is trying to close down.
Please see FAQ Item: 24. Project Newsgroups. FAQs, companion products and
other useful Project information can be seen at this web address:
http://www.mvps.org/project/.

Mike Glen
Project MVP
 

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