View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Rajesh Sivadasan Rajesh Sivadasan is offline
external usenet poster
 
Posts: 2
Default Add Menu to a VBA Form

Thanks Very much Pascal. Great Job!!!

"papou" wrote:

Hello Rajesh
Yes, here's a sample code:
'** Userform ****
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then
DisplayMenu
End If
End Sub
Private Sub UserForm_Terminate()
DeleteMenu
End Sub
'** Module ****
Const MenuName As String = "USFMenu"
Sub DisplayMenu()
DeleteMenu
Dim cb As CommandBar
Set cb = CommandBars.Add(MenuName, msoBarPopup, False, True)
With cb
With .Controls.Add(Type:=msoControlButton)
.OnAction = "Macro1"
.FaceId = 71
.Caption = "Menu 1"
End With
With .Controls.Add(Type:=msoControlButton)
.OnAction = "Macro2"
.FaceId = 72
.Caption = "Menu 2"
End With
With .Controls.Add(Type:=msoControlButton)
.OnAction = "Macro3"
.FaceId = 73
.Caption = "Menu 3"
End With
End With
Application.CommandBars(MenuName).ShowPopup
End Sub
Sub DeleteMenu()
On Error Resume Next
CommandBars(MenuName).Delete
On Error GoTo 0
End Sub
Sub Macro1()
MsgBox "Macro 1 running"
End Sub
Sub Macro2()
MsgBox "Macro 2 running"
End Sub
Sub Macro3()
MsgBox "Macro 3 running"
End Sub

HTH
Cordially
Pascal

"Rajesh Sivadasan" <Rajesh a écrit dans
le message de ...
How can I add a popup menu to a VBA Form?