Name
commandbars.Add([Name], [Position], [MenuBar], [Temporary])
Synopsis
Creates a new menu or toolbar as a CommandBar object.
|
Argument |
Settings |
|---|---|
|
|
The name of the menu or toolbar to create. |
|
|
An |
|
|
True replaces the active menu bar with the menu bar created by |
|
|
True prevents the command bar from being saved when Excel closes. False saves the command bar in the user’s .xlb file when Excel closes. Default is False. |
When creating context menus, use the Position
msoBarPopup. When creating floating toolbars, use the Position
msoBarFloating. For example, the following code creates a context menu; then displays it at the coordinates (100, 200):
Sub CreateNewContextMenu( ) Dim cb As CommandBar ' Create a new context menu bar. Set cb = Application.CommandBars.Add("ContextMenu1", _ msoBarPopup, , True) ' Add some items to the menu bar. With cb.Controls.Add(msoControlButton, , , , True) .Caption = "Sample &1" .OnAction = "Sample1" End With With cb.Controls.Add(msoControlButton, , , , True) .Caption = "Sample &2" .OnAction = "Sample2" End With With cb.Controls.Add(msoControlButton, , , , True) .Caption = "Run &All" .OnAction = "TestMenus" .BeginGroup = True End With ' Display this context menu. ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access