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

Article contributed by Ibby

This article contains instructions on how to add a right-click context menu to VBA UserForm TextBoxes. It has been tested in Word 97 and Word 2000 under Windows 98 and 2000. The menu contains Cut, Copy, Paste, Delete and SelectAll menu items.

To add this functionality to your UserForms, you need to:

1.

Download modPopupMenu.zip (3kb), which extracts to modPopupMenu.bas.

Click to download

2.

Add modPopupMenu.bas to your project: In the VBE select File | Import File.

3.

Place the following code into the MouseDown event procedure of all TextBoxes to which you want to add this function:

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
  ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    ' If right-button clicked
    If Button = 2 Then
        Call ShowPopup(Me, Me.Caption, X, Y)
    End If

End Sub

Note: This feature works best if you set the following properties for the TextBox:

   EnterKeyBehaviour = True
   MultiLine = True
   ScrollBars = fmScrollBarsBoth or fmScrollBarsVertical

This will allow pasting of large, multi-paragraph or multi-line blocks of text.