Right click menu USER FORM

lbargers

New Member
Joined
Mar 28, 2006
Messages
27
Hi all,

I have some custom VBA functionality built behind an excel worksheet. One of the features that is included is a user form that pops up when a user changes the values of certain fields. A text box on the user form will capture data that is entered by a tester, I would like to allow the testers to have the ability to copy and paste cell data into the text box on the on the user form. Currently this is only possible to paste copied data into this box using the shortcut key (Ctrl-V).

I would a like a small window or menu to appear when a tester right clicks in text box to allow them to copy or paste to this field using the mouse.


Thanks in advance for any help on this issue..

Larry
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi Larry,

Unformtunately, a Text Box doesn't have a right-click event that you can use to fire another macro and/or form. There is a double-click event that you could use.
 
Upvote 0
This will work, but you need to select an option and then OK or Enter after your selection to close the InputBox loop. That is enter your selection hit Enter or Click OK then without making a selection hit Enter or Click OK.


Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'UserForm module code, only!
Dim Message, Title, Default, myValue

Message = "Enter the item number for your choice:" & vbLf & vbLf & _
"1 ==> Add Current Selection to this TextBox!" & vbLf & _
"2 ==> Clear this TextBox!" & vbLf & vbLf & _
"9 ==> Cancel"

Title = "Menu!"
Default = "9"

myValue = InputBox(Message, Title, Default)

Select Case myValue

Case "9"
Exit Sub

Case ""
Exit Sub

Case "1"
TextBox1.Text = ActiveCell.Value
Exit Sub

Case "2"
TextBox1.Text = ""
Exit Sub

Case Else
MsgBox "Error, You entered: " & myValue & vbLf & _
"This is not one of the choices!"
Exit Sub

End Select
End Sub
 
Upvote 0
Adding a right-click context menu to VBA UserForm TextBoxes, so that you can use the right mouse button to copy, paste, etc., as you can with normal Windows dialogs

Somebody already did the work for you. It took less than two minutes to get a right-click context menu by simply following the instructions...

Textbox_ContextMenuExample.zip

I downloaded your example file and it works fine. When I try to add the code to my form though, it doesn't work.

It gives me a Type Mismatch error when it tries to set the focus on my text box.

Any ideas?
 
Upvote 0
I have no idea. Will you email me your workbook? If so, see the link below...
 
Upvote 0
I really appreciate the offer to help Right_Click.

Unfortunately, this workbook has too much proprietary information from work that I don't have the liberty to share openly.

Thanks again though.
 
Upvote 0
Didn't Juan Pablo do a blog entry some where with some examples on this?

:huh: I'd swear it was on ****'s blog, but I can't seem to find it...
 
Upvote 0
You are including the code in the standard module as well? Right?

Here is an example that I posted for some other fellow which will add a right click menu to every combobox and textbox on your userform...

Textbox_ComboBox_ContextMenuExample.zip

This is generic code that should work in any userform. If there happens to not be any textboxes or comboboxes, the code simply does nothing. Note that there are two other code modules that you must incorporate into your project...

This code goes into any userform:
<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Private</font> HoldReferences <font color="#0000A0">As</font> Collection

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> UserForm_Initialize()
       <font color="#0000A0">Dim</font> c <font color="#0000A0">As</font> Control, Rccm <font color="#0000A0">As</font> RightClickContextMenu

       <font color="#0000A0">Set</font> HoldReferences = <font color="#0000A0">New</font> Collection

       <font color="#0000A0">For</font> <font color="#0000A0">Each</font> c <font color="#0000A0">In</font> Me.Controls
           <font color="#0000A0">If</font> TypeName(c) = "TextBox" <font color="#0000A0">Or</font> TypeName(c) = "ComboBox" <font color="#0000A0">Then</font>
               <font color="#0000A0">Set</font> Rccm = <font color="#0000A0">New</font> RightClickContextMenu
               <font color="#0000A0">Call</font> Rccm.SetUp(Me, c)
               HoldReferences.Add Rccm
           <font color="#0000A0">End</font> <font color="#0000A0">If</font>
       <font color="#0000A0">Next</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("932006234013609").value=document.all("932006234013609").value.replace(/<br \/>\s\s/g,"");document.all("932006234013609").value=document.all("932006234013609").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("932006234013609").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="932006234013609" wrap="virtual">
Private HoldReferences As Collection

Private Sub UserForm_Initialize()
Dim c As Control, Rccm As RightClickContextMenu

Set HoldReferences = New Collection

For Each c In Me.Controls
If TypeName(c) = "TextBox" Or TypeName(c) = "ComboBox" Then
Set Rccm = New RightClickContextMenu
Call Rccm.SetUp(Me, c)
HoldReferences.Add Rccm
End If
Next
End Sub</textarea>

This code goes into a standard module named, "modPopupMenu":
<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#008000">'http://word.mvps.org/FAQS/Userforms/AddRightClickMenu.htm</font>

  <font color="#008000">' Required API declarations</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> CreatePopupMenu <font color="#0000A0">Lib</font> "user32" () <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> InsertMenuItem <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "InsertMenuItemA" (ByVal hMenu <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> un <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> bool <font color="#0000A0">As</font> Boolean, <font color="#0000A0">ByRef</font> lpcMenuItemInfo <font color="#0000A0">As</font> MENUITEMINFO) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> TrackPopupMenu <font color="#0000A0">Lib</font> "user32" (ByVal hMenu <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> wFlags <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> nReserved <font color="#0000A0">As</font> Long, <font color="#0000A0">ByVal</font> hwnd <font color="#0000A0">As</font> Long, lprc <font color="#0000A0">As</font> RECT) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> DestroyMenu <font color="#0000A0">Lib</font> "user32" (ByVal hMenu <font color="#0000A0">As</font> Long) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> FindWindow <font color="#0000A0">Lib</font> "user32" <font color="#0000A0">Alias</font> "FindWindowA" (ByVal lpClassName <font color="#0000A0">As</font> String, <font color="#0000A0">ByVal</font> lpWindowName <font color="#0000A0">As</font> String) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Declare</font> <font color="#0000A0">Function</font> GetCursorPos <font color="#0000A0">Lib</font> "user32" (lpPoint <font color="#0000A0">As</font> POINTAPI) <font color="#0000A0">As</font> <font color="#0000A0">Long</font>

  <font color="#008000">' Type required by TrackPopupMenu although this is ignored !!</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Type</font> RECT
           Left <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
           Top <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
           Right <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
           Bottom <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Type</font>

  <font color="#008000">' Type required by InsertMenuItem</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Type</font> MENUITEMINFO
       cbSize <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       fMask <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       fType <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       fState <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       wID <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       hSubMenu <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       hbmpChecked <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       hbmpUnchecked <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       dwItemData <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       dwTypeData <font color="#0000A0">As</font> <font color="#0000A0">String</font>
       cch <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Type</font>

  <font color="#008000">' Type required by GetCursorPos</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Type</font> POINTAPI
           x <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
           y <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Type</font>

  <font color="#008000">' Constants required by TrackPopupMenu</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> TPM_LEFTALIGN = &H0&
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> TPM_TOPALIGN = &H0
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> TPM_RETURNCMD = &H100
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> TPM_RIGHTBUTTON = &H2&

  <font color="#008000">' Constants required by MENUITEMINFO type</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MIIM_STATE = &H1
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MIIM_ID = &H2
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MIIM_TYPE = &H10
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MFT_STRING = &H0
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MFT_SEPARATOR = &H800
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MFS_DEFAULT = &H1000
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MFS_ENABLED = &H0
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> MFS_GRAYED = &H1

  <font color="#008000">' Contants defined by me for menu item IDs</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ID_Cut = 101
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ID_Copy = 102
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ID_Paste = 103
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ID_Delete = 104
  <font color="#0000A0">Private</font> <font color="#0000A0">Const</font> ID_SelectAll = 105


  <font color="#008000">' Variables declared at module level</font>
  <font color="#0000A0">Private</font> FormCaption <font color="#0000A0">As</font> <font color="#0000A0">String</font>
  <font color="#0000A0">Private</font> Cut_Enabled <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> Copy_Enabled <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> Paste_Enabled <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> Delete_Enabled <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
  <font color="#0000A0">Private</font> SelectAll_Enabled <font color="#0000A0">As</font> <font color="#0000A0">Long</font>



  <font color="#0000A0">Public</font> <font color="#0000A0">Sub</font> ShowPopup(oControl <font color="#0000A0">As</font> Object, oForm <font color="#0000A0">As</font> UserForm, strCaption <font color="#0000A0">As</font> String, x, y)

       <font color="#0000A0">Static</font> click_flag <font color="#0000A0">As</font> <font color="#0000A0">Long</font>

      <font color="#008000"> ' The following is required because the MouseDown event</font>
      <font color="#008000"> ' fires twice when right-clicked !!</font>
       click_flag = click_flag + 1

      <font color="#008000"> ' Do nothing on first firing of MouseDown event</font>
       <font color="#0000A0">If</font> (click_flag <font color="#0000A0">Mod</font> 2 <> 0) <font color="#0000A0">Then</font> <font color="#0000A0">Exit</font> <font color="#0000A0">Sub</font>

      <font color="#008000"> ' Set object reference to the textboxthat was clicked</font>
      <font color="#008000"> 'Set oControl = oForm.ActiveControl</font>

      <font color="#008000"> ' If click is outside the textbox, do nothing</font>
       <font color="#0000A0">If</font> x > oControl.Width <font color="#0000A0">Or</font> y > oControl.Height <font color="#0000A0">Or</font> x < 0 <font color="#0000A0">Or</font> y < 0 <font color="#0000A0">Then</font> <font color="#0000A0">Exit</font> <font color="#0000A0">Sub</font>

      <font color="#008000"> ' Retrieve caption of UserForm for use in FindWindow API</font>
       FormCaption = strCaption

      <font color="#008000"> ' Call routine that sets menu items as enabled/disabled</font>
       <font color="#0000A0">Call</font> EnableMenuItems(oControl, oForm)

      <font color="#008000"> ' Call function that shows the menu and return the ID</font>
      <font color="#008000"> ' of the selected menu item. Subsequent action depends</font>
      <font color="#008000"> ' on the returned ID.</font>
       <font color="#0000A0">Select</font> <font color="#0000A0">Case</font> GetSelection()
           <font color="#0000A0">Case</font> ID_Cut
               oControl.Cut
           <font color="#0000A0">Case</font> ID_Copy
               oControl.Copy
           <font color="#0000A0">Case</font> ID_Paste
               oControl.Paste
           <font color="#0000A0">Case</font> ID_Delete
               oControl.SelText = ""
           <font color="#0000A0">Case</font> ID_SelectAll
               <font color="#0000A0">With</font> oControl
                   .SelStart = 0
                   .SelLength = Len(oControl.Text)
               <font color="#0000A0">End</font> <font color="#0000A0">With</font>
       <font color="#0000A0">End</font> <font color="#0000A0">Select</font>

  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> EnableMenuItems(oControl <font color="#0000A0">As</font> Object, oForm <font color="#0000A0">As</font> UserForm)

       <font color="#0000A0">Dim</font> oData <font color="#0000A0">As</font> DataObject
       <font color="#0000A0">Dim</font> testClipBoard <font color="#0000A0">As</font> <font color="#0000A0">String</font>

       <font color="#0000A0">On</font> <font color="#0000A0">Error</font> <font color="#0000A0">Resume</font> <font color="#0000A0">Next</font>

      <font color="#008000"> ' Set object variable to clicked textbox</font>
      <font color="#008000"> 'Set oControl = oForm.ActiveControl</font>

      <font color="#008000"> ' Create DataObject to access the clipboard</font>
       <font color="#0000A0">Set</font> oData = <font color="#0000A0">New</font> DataObject

      <font color="#008000"> ' Enable Cut/Copy/Delete menu items if text selected</font>
      <font color="#008000"> ' in textbox</font>
       <font color="#0000A0">If</font> oControl.SelLength > 0 <font color="#0000A0">Then</font>
           Cut_Enabled = MFS_ENABLED
           Copy_Enabled = MFS_ENABLED
           Delete_Enabled = MFS_ENABLED
       <font color="#0000A0">Else</font>
           Cut_Enabled = MFS_GRAYED
           Copy_Enabled = MFS_GRAYED
           Delete_Enabled = MFS_GRAYED
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>

      <font color="#008000"> ' Enable SelectAll menu item if there is any text in textbox</font>
       <font color="#0000A0">If</font> Len(oControl.Text) > 0 <font color="#0000A0">Then</font>
           SelectAll_Enabled = MFS_ENABLED
       <font color="#0000A0">Else</font>
           SelectAll_Enabled = MFS_GRAYED
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>

      <font color="#008000"> ' Get data from clipbaord</font>
       oData.GetFromClipboard

      <font color="#008000"> ' Following line generates an error if there</font>
      <font color="#008000"> ' is no text in clipboard</font>
       testClipBoard = oData.GetText

      <font color="#008000"> ' If NO error (ie there is text in clipboard) then</font>
      <font color="#008000"> ' enable Paste menu item. Otherwise, diable it.</font>
       <font color="#0000A0">If</font> Err.Number = 0 <font color="#0000A0">Then</font>
           Paste_Enabled = MFS_ENABLED
       <font color="#0000A0">Else</font>
           Paste_Enabled = MFS_GRAYED
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>

      <font color="#008000"> ' Clear the error object</font>
       Err.Clear

      <font color="#008000"> ' Clean up object references</font>
      <font color="#008000"> 'Set oControl = Nothing</font>
       <font color="#0000A0">Set</font> oData = <font color="#0000A0">Nothing</font>

  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Function</font> GetSelection() <font color="#0000A0">As</font> <font color="#0000A0">Long</font>

       <font color="#0000A0">Dim</font> menu_hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">Dim</font> form_hwnd <font color="#0000A0">As</font> <font color="#0000A0">Long</font>
       <font color="#0000A0">Dim</font> oMenuItemInfo1 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oMenuItemInfo2 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oMenuItemInfo3 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oMenuItemInfo4 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oMenuItemInfo5 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oMenuItemInfo6 <font color="#0000A0">As</font> MENUITEMINFO
       <font color="#0000A0">Dim</font> oRect <font color="#0000A0">As</font> RECT
       <font color="#0000A0">Dim</font> oPointAPI <font color="#0000A0">As</font> POINTAPI

      <font color="#008000"> ' Find hwnd of UserForm - note different classname</font>
      <font color="#008000"> ' Word 97 vs Word2000</font>
       #If VBA6 <font color="#0000A0">Then</font>
           form_hwnd = FindWindow("ThunderDFrame", FormCaption)
       #Else
           form_hwnd = FindWindow("ThunderXFrame", FormCaption)
       #End <font color="#0000A0">If</font>

      <font color="#008000"> ' Get current cursor position</font>
      <font color="#008000"> ' Menu will be drawn at this location</font>
       GetCursorPos oPointAPI

      <font color="#008000"> ' Create new popup menu</font>
       menu_hwnd = CreatePopupMenu

      <font color="#008000"> ' Intitialize MenuItemInfo structures for the 6</font>
      <font color="#008000"> ' menu items to be added</font>

      <font color="#008000"> ' Cut</font>
       <font color="#0000A0">With</font> oMenuItemInfo1
               .cbSize = Len(oMenuItemInfo1)
               .fMask = MIIM_STATE <font color="#0000A0">Or</font> MIIM_ID <font color="#0000A0">Or</font> MIIM_TYPE
               .fType = MFT_STRING
               .fState = Cut_Enabled
               .wID = ID_Cut
               .dwTypeData = "Cut"
               .cch = Len(.dwTypeData)
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' Copy</font>
       <font color="#0000A0">With</font> oMenuItemInfo2
               .cbSize = Len(oMenuItemInfo2)
               .fMask = MIIM_STATE <font color="#0000A0">Or</font> MIIM_ID <font color="#0000A0">Or</font> MIIM_TYPE
               .fType = MFT_STRING
               .fState = Copy_Enabled
               .wID = ID_Copy
               .dwTypeData = "Copy"
               .cch = Len(.dwTypeData)
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' Paste</font>
       <font color="#0000A0">With</font> oMenuItemInfo3
               .cbSize = Len(oMenuItemInfo3)
               .fMask = MIIM_STATE <font color="#0000A0">Or</font> MIIM_ID <font color="#0000A0">Or</font> MIIM_TYPE
               .fType = MFT_STRING
               .fState = Paste_Enabled
               .wID = ID_Paste
               .dwTypeData = "Paste"
               .cch = Len(.dwTypeData)
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' Separator</font>
       <font color="#0000A0">With</font> oMenuItemInfo4
               .cbSize = Len(oMenuItemInfo4)
               .fMask = MIIM_TYPE
               .fType = MFT_SEPARATOR
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' Delete</font>
       <font color="#0000A0">With</font> oMenuItemInfo5
               .cbSize = Len(oMenuItemInfo5)
               .fMask = MIIM_STATE <font color="#0000A0">Or</font> MIIM_ID <font color="#0000A0">Or</font> MIIM_TYPE
               .fType = MFT_STRING
               .fState = Delete_Enabled
               .wID = ID_Delete
               .dwTypeData = "Delete"
               .cch = Len(.dwTypeData)
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' SelectAll</font>
       <font color="#0000A0">With</font> oMenuItemInfo6
               .cbSize = Len(oMenuItemInfo6)
               .fMask = MIIM_STATE <font color="#0000A0">Or</font> MIIM_ID <font color="#0000A0">Or</font> MIIM_TYPE
               .fType = MFT_STRING
               .fState = SelectAll_Enabled
               .wID = ID_SelectAll
               .dwTypeData = "Select All"
               .cch = Len(.dwTypeData)
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>

      <font color="#008000"> ' Add the 6 menu items</font>
       InsertMenuItem menu_hwnd, 1, True, oMenuItemInfo1
       InsertMenuItem menu_hwnd, 2, True, oMenuItemInfo2
       InsertMenuItem menu_hwnd, 3, True, oMenuItemInfo3
       InsertMenuItem menu_hwnd, 4, True, oMenuItemInfo4
       InsertMenuItem menu_hwnd, 5, True, oMenuItemInfo5
       InsertMenuItem menu_hwnd, 6, True, oMenuItemInfo6

      <font color="#008000"> ' Return the ID of the item selected by the user</font>
      <font color="#008000"> ' and set it the return value of the function</font>
       GetSelection = TrackPopupMenu _
                       (menu_hwnd, _
                        TPM_LEFTALIGN <font color="#0000A0">Or</font> TPM_TOPALIGN <font color="#0000A0">Or</font> TPM_RETURNCMD <font color="#0000A0">Or</font> TPM_RIGHTBUTTON, _
                        oPointAPI.x, oPointAPI.y, _
                        0, form_hwnd, oRect)

      <font color="#008000"> ' Destroy the menu</font>
       DestroyMenu menu_hwnd

  <font color="#0000A0">End</font> <font color="#0000A0">Function</font>

</FONT></td></tr></table><button onclick='document.all("932006234243640").value=document.all("932006234243640").value.replace(/<br \/>\s\s/g,"");document.all("932006234243640").value=document.all("932006234243640").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("932006234243640").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="932006234243640" wrap="virtual">
Option Explicit

'http://word.mvps.org/FAQS/Userforms/AddRightClickMenu.htm

' Required API declarations
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function InsertMenuItem Lib "user32" Alias "InsertMenuItemA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, ByRef lpcMenuItemInfo As MENUITEMINFO) As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As RECT) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

' Type required by TrackPopupMenu although this is ignored !!
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

' Type required by InsertMenuItem
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type

' Type required by GetCursorPos
Private Type POINTAPI
x As Long
y As Long
End Type

' Constants required by TrackPopupMenu
Private Const TPM_LEFTALIGN = &H0&
Private Const TPM_TOPALIGN = &H0
Private Const TPM_RETURNCMD = &H100
Private Const TPM_RIGHTBUTTON = &H2&

' Constants required by MENUITEMINFO type
Private Const MIIM_STATE = &H1
Private Const MIIM_ID = &H2
Private Const MIIM_TYPE = &H10
Private Const MFT_STRING = &H0
Private Const MFT_SEPARATOR = &H800
Private Const MFS_DEFAULT = &H1000
Private Const MFS_ENABLED = &H0
Private Const MFS_GRAYED = &H1

' Contants defined by me for menu item IDs
Private Const ID_Cut = 101
Private Const ID_Copy = 102
Private Const ID_Paste = 103
Private Const ID_Delete = 104
Private Const ID_SelectAll = 105


' Variables declared at module level
Private FormCaption As String
Private Cut_Enabled As Long
Private Copy_Enabled As Long
Private Paste_Enabled As Long
Private Delete_Enabled As Long
Private SelectAll_Enabled As Long



Public Sub ShowPopup(oControl As Object, oForm As UserForm, strCaption As String, x, y)

Static click_flag As Long

' The following is required because the MouseDown event
' fires twice when right-clicked !!
click_flag = click_flag + 1

' Do nothing on first firing of MouseDown event
If (click_flag Mod 2 <> 0) Then Exit Sub

' Set object reference to the textboxthat was clicked
'Set oControl = oForm.ActiveControl

' If click is outside the textbox, do nothing
If x > oControl.Width Or y > oControl.Height Or x < 0 Or y < 0 Then Exit Sub

' Retrieve caption of UserForm for use in FindWindow API
FormCaption = strCaption

' Call routine that sets menu items as enabled/disabled
Call EnableMenuItems(oControl, oForm)

' Call function that shows the menu and return the ID
' of the selected menu item. Subsequent action depends
' on the returned ID.
Select Case GetSelection()
Case ID_Cut
oControl.Cut
Case ID_Copy
oControl.Copy
Case ID_Paste
oControl.Paste
Case ID_Delete
oControl.SelText = ""
Case ID_SelectAll
With oControl
.SelStart = 0
.SelLength = Len(oControl.Text)
End With
End Select

End Sub

Private Sub EnableMenuItems(oControl As Object, oForm As UserForm)

Dim oData As DataObject
Dim testClipBoard As String

On Error Resume Next

' Set object variable to clicked textbox
'Set oControl = oForm.ActiveControl

' Create DataObject to access the clipboard
Set oData = New DataObject

' Enable Cut/Copy/Delete menu items if text selected
' in textbox
If oControl.SelLength > 0 Then
Cut_Enabled = MFS_ENABLED
Copy_Enabled = MFS_ENABLED
Delete_Enabled = MFS_ENABLED
Else
Cut_Enabled = MFS_GRAYED
Copy_Enabled = MFS_GRAYED
Delete_Enabled = MFS_GRAYED
End If

' Enable SelectAll menu item if there is any text in textbox
If Len(oControl.Text) > 0 Then
SelectAll_Enabled = MFS_ENABLED
Else
SelectAll_Enabled = MFS_GRAYED
End If

' Get data from clipbaord
oData.GetFromClipboard

' Following line generates an error if there
' is no text in clipboard
testClipBoard = oData.GetText

' If NO error (ie there is text in clipboard) then
' enable Paste menu item. Otherwise, diable it.
If Err.Number = 0 Then
Paste_Enabled = MFS_ENABLED
Else
Paste_Enabled = MFS_GRAYED
End If

' Clear the error object
Err.Clear

' Clean up object references
'Set oControl = Nothing
Set oData = Nothing

End Sub

Private Function GetSelection() As Long

Dim menu_hwnd As Long
Dim form_hwnd As Long
Dim oMenuItemInfo1 As MENUITEMINFO
Dim oMenuItemInfo2 As MENUITEMINFO
Dim oMenuItemInfo3 As MENUITEMINFO
Dim oMenuItemInfo4 As MENUITEMINFO
Dim oMenuItemInfo5 As MENUITEMINFO
Dim oMenuItemInfo6 As MENUITEMINFO
Dim oRect As RECT
Dim oPointAPI As POINTAPI

' Find hwnd of UserForm - note different classname
' Word 97 vs Word2000
#If VBA6 Then
form_hwnd = FindWindow("ThunderDFrame", FormCaption)
#Else
form_hwnd = FindWindow("ThunderXFrame", FormCaption)
#End If

' Get current cursor position
' Menu will be drawn at this location
GetCursorPos oPointAPI

' Create new popup menu
menu_hwnd = CreatePopupMenu

' Intitialize MenuItemInfo structures for the 6
' menu items to be added

' Cut
With oMenuItemInfo1
.cbSize = Len(oMenuItemInfo1)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = Cut_Enabled
.wID = ID_Cut
.dwTypeData = "Cut"
.cch = Len(.dwTypeData)
End With

' Copy
With oMenuItemInfo2
.cbSize = Len(oMenuItemInfo2)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = Copy_Enabled
.wID = ID_Copy
.dwTypeData = "Copy"
.cch = Len(.dwTypeData)
End With

' Paste
With oMenuItemInfo3
.cbSize = Len(oMenuItemInfo3)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = Paste_Enabled
.wID = ID_Paste
.dwTypeData = "Paste"
.cch = Len(.dwTypeData)
End With

' Separator
With oMenuItemInfo4
.cbSize = Len(oMenuItemInfo4)
.fMask = MIIM_TYPE
.fType = MFT_SEPARATOR
End With

' Delete
With oMenuItemInfo5
.cbSize = Len(oMenuItemInfo5)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = Delete_Enabled
.wID = ID_Delete
.dwTypeData = "Delete"
.cch = Len(.dwTypeData)
End With

' SelectAll
With oMenuItemInfo6
.cbSize = Len(oMenuItemInfo6)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = SelectAll_Enabled
.wID = ID_SelectAll
.dwTypeData = "Select All"
.cch = Len(.dwTypeData)
End With

' Add the 6 menu items
InsertMenuItem menu_hwnd, 1, True, oMenuItemInfo1
InsertMenuItem menu_hwnd, 2, True, oMenuItemInfo2
InsertMenuItem menu_hwnd, 3, True, oMenuItemInfo3
InsertMenuItem menu_hwnd, 4, True, oMenuItemInfo4
InsertMenuItem menu_hwnd, 5, True, oMenuItemInfo5
InsertMenuItem menu_hwnd, 6, True, oMenuItemInfo6

' Return the ID of the item selected by the user
' and set it the return value of the function
GetSelection = TrackPopupMenu _
(menu_hwnd, _
TPM_LEFTALIGN Or TPM_TOPALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, _
oPointAPI.x, oPointAPI.y, _
0, form_hwnd, oRect)

' Destroy the menu
DestroyMenu menu_hwnd

End Function
</textarea>

This goes into a class module named , "RightClickContextMenu":
<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> pTextBox <font color="#0000A0">As</font> msforms.Textbox
  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> pComboBox <font color="#0000A0">As</font> msforms.ComboBox

  <font color="#0000A0">Private</font> pUserFormReference <font color="#0000A0">As</font> UserForm

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> pComboBox_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">If</font> Button = 2 <font color="#0000A0">Then</font>
           <font color="#0000A0">Call</font> ShowPopup(pComboBox, pUserFormReference, pUserFormReference.Caption, x, y)
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> pTextBox_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, _
       <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> x <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">If</font> Button = 2 <font color="#0000A0">Then</font>
           <font color="#0000A0">Call</font> ShowPopup(pTextBox, pUserFormReference, pUserFormReference.Caption, x, y)
       <font color="#0000A0">End</font> <font color="#0000A0">If</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Public</font> <font color="#0000A0">Sub</font> SetUp(UserFormReference <font color="#0000A0">As</font> UserForm, ControlReference <font color="#0000A0">As</font> Control)
       <font color="#0000A0">Set</font> pUserFormReference = UserFormReference
       <font color="#0000A0">Select</font> <font color="#0000A0">Case</font> TypeName(ControlReference)
           <font color="#0000A0">Case</font> "TextBox": <font color="#0000A0">Set</font> pTextBox = ControlReference
           <font color="#0000A0">Case</font> "ComboBox": <font color="#0000A0">Set</font> pComboBox = ControlReference
       <font color="#0000A0">End</font> <font color="#0000A0">Select</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

</FONT></td></tr></table><button onclick='document.all("932006234351937").value=document.all("932006234351937").value.replace(/<br \/>\s\s/g,"");document.all("932006234351937").value=document.all("932006234351937").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("932006234351937").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="932006234351937" wrap="virtual">
Option Explicit

Private WithEvents pTextBox As msforms.Textbox
Private WithEvents pComboBox As msforms.ComboBox

Private pUserFormReference As UserForm

Private Sub pComboBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
If Button = 2 Then
Call ShowPopup(pComboBox, pUserFormReference, pUserFormReference.Caption, x, y)
End If
End Sub

Private Sub pTextBox_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
If Button = 2 Then
Call ShowPopup(pTextBox, pUserFormReference, pUserFormReference.Caption, x, y)
End If
End Sub

Public Sub SetUp(UserFormReference As UserForm, ControlReference As Control)
Set pUserFormReference = UserFormReference
Select Case TypeName(ControlReference)
Case "TextBox": Set pTextBox = ControlReference
Case "ComboBox": Set pComboBox = ControlReference
End Select
End Sub
</textarea>

Perhaps the code from the standard mod could be incorporated into the class as well??? Don't know... :)

Textbox_ComboBox_ContextMenuExample.zip
 
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,825
Members
452,672
Latest member
missbanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top