1

I am trying to get a simple custom ribbon button working in MS Project and seem to be getting an "Automation Error: Exception Occured" whenever I actually click the button. I do not have any "Debug" option on the error message, leading me to believe this is something internal causing the issue.

I followed a tutorial online, and everything seems straightforward enough but I cant seem to figure out why it is not working on my end.

The ribbon button is added when the project is first opened (this works correctly)

Private Sub Project_Open(ByVal pj As MSProject.Project)
On Error GoTo Open_Error
    AddTestRibbon
Exit Sub
Open_Error:
    MsgBox Err.Description
End Sub

Here is the sub that adds actually adds the ribbon

Private Sub AddTestRibbon()
    Dim customUiXml As String
 
    customUiXml = "<mso:customUI xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">" _
        & "<mso:ribbon><mso:tabs><mso:tab id=""rtpTab"" label=""RTP"" " _
        & "insertBeforeQ=""mso:TabView"">" _
        & "<mso:group id=""group1"" label=""Tools"">" _
        & "<mso:button id=""viewTasksBtn"" label=""View Synchronized Tasks"" size=""large"" " _
        & "imageMso=""GetExternalDataFromText"" onAction=""Macro10"" />" _
        & "</mso:group></mso:tab></mso:tabs></mso:ribbon></mso:customUI>"
 
    ActiveProject.SetCustomUI (customUiXml)
End Sub

And here is the "OnAction" sub. From what I can tell we do not actually get here as the error is an internal automation error

Sub Macro10(control As IRibbonControl)

    Debug.Print "Clicked"

End Sub

And here is the error message that is shown (this error message is not caught by VBA, I have it set to break on all errors and it is not breaking at any point) Error Message

Thanks in advance for any help, I am a programmer (c++ primarily) but VBA/office is definitely not my strong suit and it is likely I am doing something stupid

I have tried following other tutorials/read the documentation but they unfortunately all lead to the same result.

5
  • Its possible this line ActiveProject.SetCustomUI (customUiXml) should be either ActiveProject.SetCustomUI customUiXml or Call ActiveProject.SetCustomUI (customUiXml) Commented Aug 25, 2023 at 13:45
  • Have to say I am not an 'MS Project' user so these are just 'vague possibles' (!): ensure the Macro10 procedure is in a standard Module; ensure there is only one Macro10 procedure across ALL the loaded projects (or change its name if you think it might clash); you could try qualifying Macro10 with the name of the standard Module it is in eg onAction=""Module1.Macro10"" Commented Aug 25, 2023 at 13:57
  • Thanks for the suggestions but unfortunately both changing the initial call and giving the procedure a more complex name did not resolve the issue. I also tried moving the macro to another module and specifying that module in the onAction and that did not work either. An interesting note - removing the "onAction" from the ribbon XML makes it so clicking the button has no effect rather than the automation error, so the issue has got to be something there. Commented Aug 25, 2023 at 14:07
  • Hm. I can tell you that if I copy your RibbonXML and remove the 'mso:' texts then drop it into an Excel workbook using the Ribbon Editor, it works fine with a procedure with the signature of your Macro10. Not that saying that really helps you! I wonder if it is something more 'environmental' rather than your VBA code or RibbonXML. You could try the code in this SO answer which the guy says works and see if you have the same problem. Commented Aug 25, 2023 at 15:36
  • Thanks for linking that other question, interestingly I could not get his code to work without the "Automation Error: Exception Occured" appearing - but after some random tinkering I found removing the "IRibbonControl" parameter from my "OnAction" function in my code it working for me. Not sure why that fixed it when his code which also doesn't include that parameter did not work, but since it seems to be working now I'll just chalk it up as an oddity of MS Project. Thanks again for your help! Commented Aug 25, 2023 at 17:54

1 Answer 1

1

After doing some tinkering and being pointed in some good directions from the comments, I found that removing "control As IRibbonControl" parameter from my "onAction" procedure resolved my issue and the button is now working as expected.

The final code for Macro10 is now:

Sub Macro10()

    Debug.Print "Clicked"

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Please clarify whether the final Macro10 procedure has a parameter or not (code shows it but your comment indicates it was removed.)
Thank you for pointing that out, I made a mistake writing the answer. The final function does NOT have the parameter and removing it is what fixed the issue. I have just edited the answer for clarity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.