Search code examples
excelvbaribbonribbonx

How to I call/trigger a ribbon control sub from a regular sub?


I have a ribbon that has a button I want to call using a regular sub routine in a macro.

Public Rib As IRibbonUI
Sub RibbonOnLoad(ribbon As IRibbonUI)
    On Error GoTo this
    Set Rib = ribbon
    
    If Application.Version > 12 Then
        Rib.ActivateTab "tabColtar"
    End If
this:
End Sub

this is the button on addin i want to call:

Sub NeedToCallThis(control As IRibbonControl)
   ...
end sub

this is my sub routine i want to call/trigger/run the button from:

Sub CallingAddinButton()
    ...how to call the "NeedToCallThis" ribbon button?
end sub

Solution

  • Assuming that you are not referencing the parameter in the Sub (e.g. using a switch statement on control.ID to have the Sub do different things based on which Control on the Ribbon called it), you can just pass it Nothing, like this:

    Sub CallingAddinButton()
        NeedToCallThis Nothing
    End Sub
    

    (Note: all Properties of IRibbonControl objects are Read Only — so even if you Dim ircTemp AS IRibbonControl to create a new/virtual IRibbonControl, you cannot assign a value to ircTemp.id to pass with NeedToCallThis ircTEMP)