SOLVED

MS Project VBA to add a button to run a macro...

%3CLINGO-SUB%20id%3D%22lingo-sub-3184860%22%20slang%3D%22en-US%22%3EMS%20Project%20VBA%20to%20add%20a%20button%20to%20run%20a%20macro...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3184860%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20team%26nbsp%3B%3C%2FP%3E%3CP%3EA%20big%20ask%3A%26nbsp%3B%20Does%20anyone%20have%20some%20VBA%20code%20that%20adds%20a%20button%20to%20a%20tab%20and%20assigns%20to%20a%20macro%20when%20a%20project%20file%20is%20open%2C%20and%20then%20hides%20when%20the%20macro%20is%20closed.%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20if%20I'm%20allowed%20to%20say%20it%20on%20this%20forum%20but%20happy%20to%20pay%20a%20small%20donation%20as%20I%20know%20the%20effort%20involved.%3C%2FP%3E%3CP%3EMany%20thanks%26nbsp%3B%3CBR%20%2F%3EJames%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3184944%22%20slang%3D%22en-US%22%3ERe%3A%20MS%20Project%20VBA%20to%20add%20a%20button%20to%20run%20a%20macro...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3184944%22%20slang%3D%22en-US%22%3EJames_Price%2C%3CBR%20%2F%3EI%20remember%20there%20was%20an%20approach%20detailed%20in%20the%20no%20archived%20MSDN%20Project%20customization%20and%20programming%20forum%20but%20I%20can't%20locate%20it.%20However%2C%20you%20may%20find%20this%20thread%20useful%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fsocial.msdn.microsoft.com%2FForums%2Fen-US%2F32d6a437-f9da-43fd-aa98-1c1740a1c798%2Fadd-button-in-ribbon-dynamically%3Fforum%3Dproject2010custprog%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsocial.msdn.microsoft.com%2FForums%2Fen-US%2F32d6a437-f9da-43fd-aa98-1c1740a1c798%2Fadd-button-in-ribbon-dynamically%3Fforum%3Dproject2010custprog%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EJohn%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3184982%22%20slang%3D%22en-US%22%3ERe%3A%20MS%20Project%20VBA%20to%20add%20a%20button%20to%20run%20a%20macro...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3184982%22%20slang%3D%22en-US%22%3EMany%20thanks%20for%20this%20John%20but%20this%20looks%20like%20adding%20a%20ribbon%20in%20Project%20Server%20with%20Visual%20Studio.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3185504%22%20slang%3D%22en-US%22%3ERe%3A%20MS%20Project%20VBA%20to%20add%20a%20button%20to%20run%20a%20macro...%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3185504%22%20slang%3D%22en-US%22%3EJames%2C%3CBR%20%2F%3ENow%20that%20I%20re-read%20your%20post%2C%20even%20the%20other%20MSDN%20posting%20I%20mentioned%20required%20Visual%20Studio%20and%20I%20see%20you're%20asking%20for%20a%20VBA%20approach.%20I%20know%20of%20no%20way%20to%20use%20VBA%20itself%20to%20do%20what%20you%20want.%3CBR%20%2F%3E%3CBR%20%2F%3EWhat%20I%20have%20on%20my%20system%20are%20buttons%20on%20the%20Quick%20Access%20toolbar%20or%20a%20custom%20tab%20on%20the%20ribbon%20that%20activates%20oft-used%20macros.%3CBR%20%2F%3E%3CBR%20%2F%3EJohn%3C%2FLINGO-BODY%3E
Contributor

Hi team 

A big ask:  Does anyone have some VBA code that adds a button to a tab and assigns to a macro when a project file is open, and then hides when the macro is closed. 

Not sure if I'm allowed to say it on this forum but happy to pay a small donation as I know the effort involved.

Many thanks 
James 

12 Replies
James_Price,
I remember there was an approach detailed in the no archived MSDN Project customization and programming forum but I can't locate it. However, you may find this thread useful:
https://social.msdn.microsoft.com/Forums/en-US/32d6a437-f9da-43fd-aa98-1c1740a1c798/add-button-in-ri...

John
Many thanks for this John but this looks like adding a ribbon in Project Server with Visual Studio.
James,
Now that I re-read your post, even the other MSDN posting I mentioned required Visual Studio and I see you're asking for a VBA approach. I know of no way to use VBA itself to do what you want.

What I have on my system are buttons on the Quick Access toolbar or a custom tab on the ribbon that activates oft-used macros.

John
Hi John
Actually there is. I did it many years ago but didn't keep the code... D'oh. It uses XML to do this.
Cheers
James
James,
Are you sure you did it with the ribbon GUI or was it perhaps with the pre-Project 2010 menu driven interface? The old menu driven interface was a lot more flexible. If it was with the ribbon GUI I'd be interested to hear (as might other users) on how XML played into the process.
John
Hi John - the old menus were a joy to change with VBA :-). I found this to give you a flavour of what needs to be done:

Private Sub AddEVMRibbon()
Dim ribbonXml As String

ribbonXml = "<mso:customUI xmlns:mso="http://schemas.microsoft.com/office/2009/07/customui">"
ribbonXml = ribbonXml + " <mso:ribbon>"
ribbonXml = ribbonXml + " <mso:qat/>"
ribbonXml = ribbonXml + " <mso:tabs>"
ribbonXml = ribbonXml + " <mso:tab id=""macroTab"" label=""EVM"" insertAfterQ=""mso:TabFormat"">"
ribbonXml = ribbonXml + " <mso:group id=""testGroup"" label=""Test"" autoScale=""true"">"
ribbonXml = ribbonXml + " <mso:button id=""export_to_excel"" label=""Set EVM Calc Method"" "
ribbonXml = ribbonXml + "imageMso=""DiagramTargetInsertClassic"" onAction=""Set_EVM_Method""/>"
ribbonXml = ribbonXml + " <mso:button id=""get_evm_method"" label=""EVM to Excel"" "
ribbonXml = ribbonXml + "imageMso=""DiagramTargetInsertClassic"" onAction=""evm_to_excel""/>"
ribbonXml = ribbonXml + " </mso:group>"
ribbonXml = ribbonXml + " </mso:tab>"
ribbonXml = ribbonXml + " </mso:tabs>"
ribbonXml = ribbonXml + " </mso:ribbon>"
ribbonXml = ribbonXml + "</mso:customUI>"

ActiveProject.SetCustomUI (ribbonXml)

End Sub



James,
Thanks, I'll have to try this when I get some time.

Update: Just for reference, the link provided for the schema is no longer active.
John

Hi John - that's really strange as we are using it fine.
James,
Are you sure it copied correctly when you posted? When I click on the link as it appears in your post, I get the message, "the resource you are looking for has been removed, had its name changed, or is temporarily unavailable." Same thing happens if I copy the link and paste into a new browser window.
John
Sorry John I can't explain it. We are using it in the VBA editor and the macro is working file.
James
James,
I tried a different approach. I pasted the code you posted into a Project file (Project Pro 2019). I got a compile error but once I removed the quotes ("") from the http... string, the macro ran but didn't appear to do anything. I got some flashing of the ribbon when the last line of code executed but that was it. I must be missing something. For reference, I've never worked with XML code so I'm sure that has something to do with it.
John
best response confirmed by James_Price (Contributor)
Solution
Hi John - I'm using MS Project Online Desktop client. Below is the code that is working for me. Like you I'm not an xml person but this works for me.

' Name Proc for Error Handler Message
strProcedure = "sAddCustomRibbon"

Dim ribbonXml As String

ribbonXml = "<mso:customUI xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">"
ribbonXml = ribbonXml + " <mso:ribbon>"
ribbonXml = ribbonXml + " <mso:qat/>"
ribbonXml = ribbonXml + " <mso:tabs>"
ribbonXml = ribbonXml + " <mso:tab id=""tabCustom"" label=""Split / Collapse Tasks"" insertAfterQ=""mso:TabFormat"">"
ribbonXml = ribbonXml + " <mso:group id=""grpSplitCollapse"" label=""Split / Collapse"" autoScale=""true"">"
ribbonXml = ribbonXml + " <mso:button id=""btnSplitTasks"" label=""Split Tasks"" size=""large"" imageMso=""CellsDelete"" onAction=""sSplitTasks""/>"
ribbonXml = ribbonXml + " <mso:button id=""btnCollapseTasks"" label=""Collapse Tasks"" size=""large"" imageMso=""CellsInsertDialog"" onAction=""sCollapseTasks""/>"
ribbonXml = ribbonXml + " </mso:group>"
ribbonXml = ribbonXml + " </mso:tab>"
ribbonXml = ribbonXml + " </mso:tabs>"
ribbonXml = ribbonXml + " </mso:ribbon>"
ribbonXml = ribbonXml + "</mso:customUI>"

ActiveProject.SetCustomUI (ribbonXml)