Forum Discussion
James_Price
Feb 18, 2022Brass Contributor
MS Project VBA to add a button to run a macro...
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...
- Feb 24, 2022Hi 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)
James_Price
Brass Contributor
Many thanks for this John but this looks like adding a ribbon in Project Server with Visual Studio.
John-project
Feb 18, 2022Silver Contributor
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
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
- James_PriceFeb 20, 2022Brass ContributorHi 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- John-projectFeb 20, 2022Silver ContributorJames,
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- James_PriceFeb 20, 2022Brass ContributorHi 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