Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POPUP menu

Status
Not open for further replies.

Xkarlos77

Programmer
Jan 19, 2003
28
CZ
Is it possible to have
a POP UP menu in a form ??

thank you
 
Create a module and place this code inside it:
Code:
Const PopUpName As String = "MyPopupMenu"

Sub DeletePopupMenu()
    CommandBars(PopUpName).Delete
End Sub

Sub CreatePopupMenu()
Dim cb As CommandBar, cbp As CommandBarPopup
    DeletePopup
    Set cb = CommandBars.Add(PopUpName, msoBarPopup, False, True)
    With cb
        With .Controls.Add(Type:=msoControlButton)
            .OnAction = "MethodIWishToExecute"
            .FaceId = 71
            .Caption = "Menu Item 1"
            .TooltipText = "Tooltip text for menu item 1"
        End With
    
        Set cbp = .Controls.Add(Type:=msoControlPopup)
        With cbp
            .BeginGroup = True
            .Caption = "Sub Menu"
            With .Controls.Add(Type:=msoControlButton)
              .OnAction = "MethodIWishToExecute"
              .FaceId = 71
              .Caption = "Submenu Item 1"
              .TooltipText = "Tooltip text for submenu item 1"
            End With
        End With
        Set cbp = Nothing
    End With
    Set cb = Nothing
End Sub

Sub DisplayPopUpMenu()
    Application.CommandBars(PopUpName).ShowPopup
End Sub


Sub MethodIWishToExecute()
    MsgBox "Put your macro code here!"
End Sub

Then after creating a userform, double-click it and you'll be taken to the code editor. From the right most combo box select the MouseDown event and place this code in there:
Header:
Code:
 Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Body:
Code:
  If Button = 2 Then 'if right mouse-button is clicked then...
        DisplayPopUpMenu
  End If
Clive [infinity]
 
Sorry forgot to mention something....
call the CreatePopupMenu method from the ThisWorkbook Open event and the DeletePopupMenu method from the ThisWorkbook BeforeClose event. Clive [infinity]
 
I have problem,

I don't have a library (reference) with CommandBar,
I use VBA for AutoCAD, what reference can I use ???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top