To show a popmenu in a VBA form, use the form MouseDown event.
Here is the code.
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then 'the right mouse-button was clicked
DisplayYourPopUpMenu
End If
End Sub
I was trying to do it from Word VBA and you do not have the menu builder functionality that you have in VB. Is there a way to do this? I ended up using a combobox to display options but I really would've preferred the VB type menu. You can build a Word Toolbar or commandbar but it is not accessible when a VBA form is loaded.
I got this piece of code from the Web ( can't remember where ). It display a form in Word and when the mouse is right-clicked (within the form) a popup menu appears. Here is the code.
The form consists of a Label and a Close button.
Private Sub btnClose_Click()
Me.Hide
End Sub
' each object in the UserForm can have it's own popup menu assigned to it.
Private Sub Label1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then ' the right mouse-button was clicked
DisplayCustomPopUp
End If
End Sub
' the UserForm can have it's own popup menu assigned to it.
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then ' the right mouse-button was clicked
DisplayCustomPopUp
End If
End Sub
The code in Module1
Option Explicit
Const PopUpCommandBarName As String = "TemporaryPopupMenu"
Sub DeletePopUp() ' deletes the custom popup menu
On Error Resume Next
CommandBars(PopUpCommandBarName).Delete
On Error GoTo 0
End Sub
Sub CreatePopUp() ' creates the custom popup menu
Dim cb As CommandBar
DeletePopUp
Set cb = CommandBars.Add(PopUpCommandBarName, msoBarPopup, False, True)
With cb
With .Controls.Add(Type:=msoControlButton)
.OnAction = "MyMacroName"
.FaceId = 71
.Caption = "Custom Menu 1"
.TooltipText = "Custom Tooltip Text 1"
End With
With .Controls.Add(Type:=msoControlButton)
.OnAction = "MyMacroName"
.FaceId = 72
.Caption = "Custom Menu 2"
.TooltipText = "Custom Tooltip Text 2"
End With
With .Controls.Add(Type:=msoControlButton)
.OnAction = "MyMacroName"
.FaceId = 73
.Caption = "Custom Menu 3"
.TooltipText = "Custom Tooltip Text 3"
End With
End With
Set cb = Nothing
End Sub
Sub DisplayCustomPopUp() ' displays the custom popup menu
Application.CommandBars(PopUpCommandBarName).ShowPopup
End Sub
Sub DisplayExampleUserForm() ' displays the form
Load UserForm1
UserForm1.Show
Unload UserForm1
End Sub
Sub MyMacroName() ' dummy macro attached to the popup menu items
MsgBox "This could be your macro running!", vbInformation, ThisDocument.Name
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.