PDA

View Full Version : Right-Click on Toolbar button



gonen
05-01-2011, 10:20 PM
Hello

I create my toolbar using the code below


MacNames(0) = "MoveSelectedUp"
TipText(0) = MacNames(0) + " vbcrlf vbcrlf Move Up Selected rows"
iconnum(0) = 38
MacNames(1) = "MoveSelectedDown"
TipText(1) = MacNames(1) + " vbcrlf vbcrlf Move Down Selected rows "
iconnum(1) = 40
MacNames(2) = "CloneSelected"
TipText(2) = MacNames(2) + " vbcrlf vbcrlf Clone Selected rows"
iconnum(2) = 29



For iCtr = LBound(MacNames) To UBound(MacNames)
tt = TipText(iCtr)
tt = Replace(tt, "vbcrlf", vbCrLf)
With .Controls.add(Type:=msoControlButton)
.OnAction = "'" & ThisWorkbook.Name & "'!" & MacNames(iCtr)
.Style = msoButtonIconAndCaption
.FaceId = iconnum(iCtr)
.TooltipText = tt
End With
Next iCtr


My Questions are:
1 - When I click on a toolbar button - the assigned macro is invoked. Can I invoke a different macro when I right-click on a toolbar button ?

2 - Is there a way to create a menu that will be displayed when I right-click on a toolbar button ? Currently, when I right click any of my toolbar buttons, I get the menu of "Standard Fornmatting..." which is actually as if I clicked on "View >> Toolbars"



Thanks for any help

mikerickson
05-01-2011, 11:14 PM
Instead of adding a command button to the control bar, you could add a dropdown menu of routines to be selected.

This code takes an existing command bar ("Custom 1") and adds a menu with two sub items to be selected.

Sub test()
With Application.CommandBars("Custom 1")

Rem add menu type control to CommandBar
With .Controls.Add(Type:=msoControlPopup, temporary:=True)
.Caption = "New Menu"

Rem add first menu item
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "First Routine"
.FaceId = 162
.OnAction = "Macro1"
End With

Rem add second menu item
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "Second Routine"
.OnAction = "Macro2"
End With

End With
End With
End Sub

Sub Macro1()
MsgBox "1"
End Sub
Sub macro2()
MsgBox "2"
End Sub

gonen
05-01-2011, 11:52 PM
This is real nice !

but

it opens the menu when I click on the button....

I am looking to run a macro on click and open the menu on right-click

any idea ???

many thanks