Search code examples
vbaoutlooktoggleimagebutton

Outlook Ribbon: button toggle in VBA


I have written a listener in vba, inside my outlook, to perform an action when I receive a mail.

I would like to be able to turn this feature on and off using a button on my Outlook ribbon.

To know the current state (on/off), I would like to toggle between two images on the button (green/red).

Does anyone know how to alter the image on the button when it has been clicked?


Solution

  • I don't know of a way to do this specifically, or if it's even possible, but you can edit a Group's visible property at runtime. You could therefore have two groups, with two images - one red, one green. In this case, I have a group called Actions, and I will show how to make it visible/invisible:

    XML - Place your button as a control in here

    <group id="customGroup1" label="Actions" getVisible="CallbackGetVisible">
    
    </group>
    

    Public/Module level declaration. This will determine whether the group is visible or not

    Public bShowActions As Boolean
    

    CallbackGetVisible is called by the XML to set the visible property of the group. The visibility is determined by bShowActions

    Sub CallbackGetVisible(control As IRibbonControl, ByRef visible)
    On Error GoTo err_Handle
    Const strError As String = "'CallbackGetVisible'"
    
        'Don't Re-Set The Value If It's Already The Same
        If bShowActions = visible Then GoTo Cleanup
        visible = bShowActions
        GoTo Cleanup
    
    'Set Any Objects to Nothing, Exits Routine
    Cleanup:
        Exit Sub
    'Throw Error
    err_Handle:
        'Handle your error here
        Resume Cleanup
    End Sub
    

    It's not straightforward, but you should be able to achieve what you need.