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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Createing userform Popup Menus

Status
Not open for further replies.

caguila

MIS
Mar 20, 2002
28
0
0
GB
Can you create popupmenus or form menus like you can in VB in VBA userforms?
 
From what application? Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
Hi

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


Hope this is what you want.

rgrds
LSTAN
 
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.

??
Christel
 
Hi caguila

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


Hope this is what you're refering.

rgrds
LSTAN


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top