Search code examples
vbams-officeribbon

Checkbox in Ribbon who maintain the value


I add a checkBox in the ribbon using xml (with Custom UI Editor for Microsoft Office) That exec vba code. I need this checkBox to maintain the value (checked or unchecked) even if I close the application. Right now when I close and open it, the checkBox appears always unchecked.

I also need to know if it is possible to know if this checkbox is checked or not using vba


Solution

  • To make it simpler, here is what you should do. You need to decide how you will be returning or storing the value. Whether to use XML/Registry/CustomXML etc etc. Once you have decided, do these steps.

    Ribbon XML:

    <checkBox id="cbStoreValue" label="MyCheckBox" getPressed="Function_Clicked" onAction="Function_Action" />
    

    VBA Code:

    Public Function Function_Clicked(control As IRibbonControl, ByRef pressed)
        pressed = GetKey
    End Function
    
    Public Function Function_Action(control As IRibbonControl, pressed As Boolean)
        Store pressed
    End Function
    
    Public Sub Store(value As Boolean)
        '''write the code for storing the key, may be to an ini file, or registry or an external xml, custom xml or custom document property
    End Sub
    
    Public Function GetKey() As Boolean
        '''write the code for getting the key back from the source which you might have used to store the value.
        '''return the correct value here
        GetKey = True ' or whatever you have selected previously
    
    End Function
    

    Hope this helps :)

    Vikas B