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
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
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
John-project
Feb 20, 2022Silver Contributor
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
- James_PriceFeb 24, 2022Brass ContributorHi 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) - John-projectFeb 23, 2022Silver ContributorJames,
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 - James_PriceFeb 23, 2022Brass ContributorSorry John I can't explain it. We are using it in the VBA editor and the macro is working file.
James - John-projectFeb 23, 2022Silver ContributorJames,
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 - James_PriceFeb 23, 2022Brass ContributorHi John - that's really strange as we are using it fine.