Button to run Macro

G

Gary Shell

I have created a macro to isolate a specific set of tasks in a Project Gant
Chart view. I want to add a button of some sort to the project file to
execute this macro. Of course I can do this using a custom toolbar. Trouble
is that adds the toolbar to the Global.mpt and consequently EVERY Project
file I open has the toolbar. I don't want this. I want to add a button
to just ONE Project file.

This project file is actually being created by a VB.NET application which
instantiates MS Project, creates all the tasks with predecessor info and
then using this bit of code, it adds my macro to the instantiated Project
file:
----------
'point to this assembly
Dim thisExe As System.Reflection.Assembly
thisExe = System.Reflection.Assembly.GetExecutingAssembly()

'get the macro text from the resources
Dim ResourceStream As System.IO.Stream =
thisExe.GetManifestResourceStream("MSProjectExport.TraceMacro.txt")

'load the stream into a stream reader
Dim ResourceStreamReader As System.IO.StreamReader = New
System.IO.StreamReader(ResourceStream)

'get the text into a string
Dim theMacroText As String = ResourceStreamReader.ReadToEnd
Dim theVBProject As Microsoft.Vbe.Interop.VBProject

For Each theVBProject In ProjectApp.VBE.VBProjects
'note that we need to point to the LAST VBProject, the first one is the
'Global Project and would apply to all projects the user saves. We only
'want the macro to be instantiated on this particular project file which
'would be the last one. This takes into account the fact that the user
'MIGHT have another Project file open.
Next

Dim theVBECodemodule As Microsoft.Vbe.Interop.CodeModule
'point to the code module in the MSProject file
theVBECodemodule = theVBProject.VBComponents.Item(1).CodeModule

'add the macro code to the project file
theVBECodemodule.AddFromString(theMacroText)

-----------

I then want to create SOME sort of button and add code to that button to
execute my macro. But once again I only want this button to appear in this
single Project file. I do NOT want it to be part of the Global Project
Template.

Am I dreaming, or can this be done.

I thought I might add some code to the OPEN event to create a custom toolbar
and other code to the BEFORE Close event to delete the custom tool bar, but
that seems like such a kludge.

Any ideas?

Gary Shell
 
M

Mike Glen

Hi Gary,

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

Mike Glen
Project MVP
 
G

Gary Shell

Jan,

I don't understand what you are suggesting. It's not that I don't want the
custom toolbar to SHOW on every project. I don't want it to exist. I was
hoping there is some way to create a custom toolbar on a SPECIFIC project
file. Reading between the lines of some other messages about creating
toolbars in the file open event and destroying them in the before close
event, I suspect that there is no other way to do what I want.

Gary
 
J

Jan De Messemaeker

Hi,

You can copy it to a file through the organizer but AFAIK it won't show
then.
They only seem to show when they're in the Global.
HTH
 
G

Gary Shell

Actually Jan I did find a way to do what I want. It is possible to specify
in the CommandBar.Add method that the item you are creating is to be
Temporary.
Here is a sample:
CommandBars.Add(Name:="MyTools", Temporary:=True,
Position:=msoBarTop).Visible = True

'add a button to the command bar for the macro
CommandBars("MyTools").Controls.Add Type:=msoControlButton, ID:=40001,
Before:=1, Parameter:="Macro ""ResetFilter"""
CommandBars("MyTools").Controls.Item(1).Caption = "Show All Processes"
CommandBars("MyTools").Controls.Item(1).Style = msoButtonCaption


I use this code to create the command bar on the project after I add the
code module containing my macro code. I also add the same code to the
Project_Open event, so that each time the Project file is opened the command
bar is recreated.

Gary Shell
 
J

Jan De Messemaeker

Excellent!
After dwelling in these groups for over 5 yrs now I keep learning stuff.
Thanks!
 
M

Mike Glen

Oh! you cross-posted! It's the vba one that's closing as it says in the
FAQ.


Mike Glen
Project MVP
 
J

John

Mike Glen said:
Oh! you cross-posted! It's the vba one that's closing as it says in the
FAQ.


Mike Glen
Project MVP


Mike,
Wake up Mike, wake up. I know, it's Monday for all of us (except maybe
Rod) :)

John
 

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