Autorun question

J

jennifer.lyon

VBA newbie here... self-taught by reading code and playing around with
little snippets.

Trying to make a little bit of VBA code/macro that will run when a
series of Project files are opened, to check if certain columns are
visible on the Task Entry view, and if not, display them.

So first I made 2 little subs (one for each column) that uses very
elementary error handling after attempting to select the column in
question (eventually I'll redo this to avoid the "GoTo"). An example
of one of the subs is below...the other sub is identical except for the
name of the label, and name of the column (which is "Text2" with title
renamed to "Contractor")

Sub ShowPredecessors()

On Error GoTo ShowPredecessorColumn
SelectTaskColumn Column:="Predecessors"
MsgBox "Predecessors Column Already There"
Exit Sub

ShowPredecessorColumn:
SelectTaskColumn Column:="Resource Names"
TableEdit Name:="&Entry", TaskTable:=True, NewName:="",
FieldName:="", _
NewFieldName:="Predecessors", Title:="", Width:=10,
Align:=2, _
ShowInMenu:=True, LockFirstColumn:=True, DateFormat:=255,
RowHeight:=1, _
ColumnPosition:=6, AlignTitle:=1
TableApply Name:="&Entry"
MsgBox "Added Predecessors Column"


End Sub

Okay... as stand-alone subs, running from the VB editor, they worked
fine.

Went to follow the suggestion for an Autorun, and created the following
in the "Microsoft Project Objects" tab of my GLOBAL.MPT

Private Sub Project_Open(ByVal pj As Project)
ShowPredecessors
ShowContractor

End Sub


In all cases, the MsgBox calls are for validation/checking only without
using the watch/debug window.

Now... when I open the Project file, it appears the code gets run
twice. If the columns are already showing, I get the message box that
the Predecessors Column is already there, then a message box that the
Contractor column is already there, then ANOTHER message box that the
Predecessors column is already there, and a final message box for the
Contractor column.

It seems to me that both subs are being called first from the
Project_Open sub in the GLOBAL.MPT and then each individually from
their definition in the Modules section of the GLOBAL.MPT.

Is this correct?

I would like the routines to run once and once only when the file is
open. If one or both columns aren't showing, then show them. If
they're showing, then nothing else needs to happen.

Suggestions for improvement and stupid comments gladly accepted in this
case. Like I said, I self-taught myself the VBA in these routines
today as I worked on them.

--jennifer
 
J

JackD

It may be opening twice because it is opening for global.mpt and whatever
other file you are opening (or perhaps Project1 - which is usually created
at start-up) You can debug by adding the line:

msgbox ActiveProject.Name

and see what is actually happening.
 
J

jsl

That makes sense.... I figured it was something stupid I was doing as a
newbie to VBA.
 
J

jsl

Updated reply... Yes that was the problem. I moved everything out of
the GLOBAL.MPT into the actual project's space and it worked like a
champ.

Now onward to bigger and better challenges in my quest to learn VBA for
MS Project.
 
J

jsl

Yes!

And I just talked to one of my other customers on this project, and he
wants to expand the code so that it will show only a pre-defined set of
columns as the default, rather than just leaving the schedule showing
whichever columns one of the other customers decided they wanted to
see.

My task after that is to automate the checking of the filename links
for the various linked project files we have in the master project.
This since they insist on copying these files all over kingdom come on
a regular basis, which seems to randomly munge the folder part of the
filenames and occasionally leave the links pointing to obsolete
filenames.

Lots of VBA exposure in my near future.... but I doubt I will be a
specialist anytime soon. This is the first real programming I've done
in about 15 years other than some very simple Excel macros.
 
J

JackD

--
-Jack ... For project information and macro examples visit
http://masamiki.com/project

..
jsl said:
Yes!

And I just talked to one of my other customers on this project, and he
wants to expand the code so that it will show only a pre-defined set of
columns as the default, rather than just leaving the schedule showing
whichever columns one of the other customers decided they wanted to
see.


Just predefine a particular view and use code to reset it if necessary. You
can do most of this with the macro recorder.
 

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