user2284200
user2284200

Reputation: 23

RibbonX OnAction: Wrong number of arguments or invalid property assignment

I'm using the RibbonX Visual Designer 2010 add-in from Andy Pope to create a custom tab, groups, and buttons. I already have all kinds of macros as VBA code in an XLAM workbook that will be open on startup. The problem I have is that I cannot figure out how to call the procedure from any of these buttons. RibbonX Visual Designer provides a property form for a button with an OnAction field. I have tried

ProcedureName

"ProcedureName"

"'spreadsheetname.xlam'!ProcedureName"

All I get is an error message "Wrong number of arguments or invalid property assignment". This procedure takes no arguments, so that's not the problem. The attached image shows what the property form looks like and the OnAction field.

Property form with OnAction Field

Can someone give me a clue about the form of the procedure call here?

Upvotes: 1

Views: 1340

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

For ribbon buttons the callback should look like the following one:

C#: void OnAction(IRibbonControl control)

VBA: Sub OnAction(control As IRibbonControl)

C++: HRESULT OnAction([in] IRibbonControl *pControl)

Visual Basic: Sub OnAction(control As IRibbonControl)

You can all callbacks described for all kind of ribbon controls in the following articles:

Upvotes: 3

Excelosaurus
Excelosaurus

Reputation: 2849

I should have read your question more attentively. The key was "This procedure takes no arguments".

In Ribbon Editor, just provide

NumberFormatTwoDecimals

for OnAction, and also make sure your sub is declared as follows:

Public Sub NumberFormatTwoDecimals(control As IRibbonControl)
    'Your code goes here...
End Sub

Excel expects you to make room for a control parameter that it systematically passes to the sub you specify, even if you don't have any use for said parameter.

Developers often specify the same sub in many OnAction properties for different controls, because once in their sub, they can check the ID and/or the Tag property of the control parameter to decide what to do:

Public Sub MyActionHandler(control As IRibbonControl)
    Select Case control.ID
        Case "MyButton1": HandleMyButton1
        Case "MyButton2": HandleMyButton2
        Case Else: MsgBox "?!?"
    End Select
End Sub

With the latter approach, you don't have to modify your existing code base to accomodate the control parameter.

Upvotes: 4

Related Questions