Search code examples
vbaexcel-2010ribbon

Updating contents of an iRibbon control from another control's callback


Back in the old days of VBA, we used to be able to directly access controls on a form by name. I'm a little perplexed by how to do this in VBA 2010 (or if it's even possible any longer).

Say I have a ribbon control with a dropdown list called "channelList", and elsewhere on the ribbon I have a textbox called "labelText". Each of these items has a unique callback function - when I type something into labelText, its callback fires, and when I select an item from the channelList listbox, its callback fires with the list item passed as an argument.

Where I'm stumped by is how I would update the labelText textbox contents with 'something' from within the channelList callback.

Is there a way to directly access the textbox control from within the listbox callback, or must I generate some sort of event? What method or function would I use to set the text value for the control? (Do I need to cast the IRibbonControl object to something?)


Solution

  • The solution was a combination of an answer and the comments, so here goes:

    • Add a "getText" callback subroutine name to the Ribbon XML, specific to the "labelText" editbox.
        editBox id="labelText" label="Text:" sizeString="xxxxxxxxxx"
        onChange="TextboxCallback" getText="UpdateTextBoxText"
        screentip="Channel label"
        supertip="Limited to 10 characters. Press Enter once finished typing." 
    
    • In the combo box callback function, set the desired editbox text to a global and call Ribbon.InvalidateControl with "labelText" as the argument.
        MyRibbon.InvalidateControl "labelText"
    
    • Implement the editbox callback, passing a handle to the ribbon and another variable ByRef to contain the 'stuff' to be updated. Use the global to update the control's text via the ByRef argument.
        Sub UpdateTextBoxText(control As IRibbonControl, ByRef returnedVal)
        Select Case (control.id)       
            Case "labelText"
                returnedVal = LabelText
        End Select
        End Sub
    

    I guess this is "progress". labelText.Value = "blah" is far too simple.