Search code examples
excelvbaribbonxcommandbaroffice-2019

Adding 'icon only' buttons to a custom context menu


Using Office 2019, Excel

How can I add 'icon only' buttons to a custom context?

I have this simple menu:

On Error Resume Next
CommandBars("MyBar").Delete

Dim cb As CommandBar
Set cb = CommandBars.Add("MyBar", msoBarPopup, , True)
Dim bItem As CommandBarControl, sbItem As CommandBarControl

Set bItem = cb.Controls.Add(msoControlButton, , , , True)
With bItem
     .Caption = "Complete": .FaceId = 1907: .OnAction = "CompleteItm"
End With

Set bItem = cb.Controls.Add(msoControlButton, , , , True)
With bItem
     .Caption = "Cancel": .FaceId = 478: .OnAction = "CancelItm"
End With

Set bItem = cb.Controls.Add(msoControlButton, , , , True)
With bItem
     .Caption = "Remove": .FaceId = 67: .OnAction = "RemoveItm"
End With

Set bItem = cb.Controls.Add(msoControlButton, , , , True)
With bItem
     .Caption = "On hold": .OnAction = "PutOnHold": .FaceId = 463
End With

Set bItem = cb.Controls.Add(msoControlPopup, , , , True)
With bItem
     .Caption = "Add"
     
     Set sbItem = bItem.Controls.Add(msoControlButton, , , , True)
     With sbItem
           sbItem.Caption = "ADA.regular": .OnAction = "ARA_REG": .FaceId = 244
     End With
     Set sbItem = bItem.Controls.Add(msoControlButton, , , , True)
     With sbItem
           sbItem.Caption = "ADA.unique": .OnAction = "ARA_UNI": .FaceId = 244
     End With
     Set sbItem = bItem.Controls.Add(msoControlButton, , , , True)
     With sbItem
           sbItem.Caption = "ADX.dynamic": .OnAction = "ARX_DYN": .FaceId = 216
     End With
     
End With

CommandBars("MyBar").ShowPopup

what I want is a more compact version of it where all first 4 items are only icons - lined up in one row (as opposed to 4 menu items stacked up with each a caption and an icon).

I tried different control settings (using CommandnbarButton) and other configurations where msoButtonIcon is available but never succeeded. I think the BarPopup type bar doesn't support such customization. Or maybe I'm wrong...?


Solution

  • CommandBars were deprecated and may be used nowadays mostly for executing built-in UI elements programmatically by calling the CommandBars.ExecuteMso method. Starting from Office 2007 a new Ribbon UI was introduced.

    Use the Fluent UI (aka Ribbon UI) which provides all the required features for setting up the layout of your controls as well as placing them into contextual tabs. Read more about the Ribbon UI in the following series of articles: