Option Explicit
Private Sub [COLOR=#ff0000][B]TextBox1[/B][/COLOR]_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call ShowMenu([COLOR=#ff0000][B]TextBox1[/B][/COLOR], Button, Shift, X, Y)
End Sub
Private Sub [COLOR=#ff0000][B]TextBox2[/B][/COLOR]_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call ShowMenu([COLOR=#ff0000][B]TextBox2[/B][/COLOR], Button, Shift, X, Y)
End Sub
Private Sub [COLOR=#ff0000][B]TextBox3[/B][/COLOR]_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Call ShowMenu([COLOR=#ff0000][B]TextBox3[/B][/COLOR], Button, Shift, X, Y)
End Sub
[COLOR=#008000][B]' Add here 'MouseUp' event for the rest of textboxes in the same fashion as above[/B][/COLOR]
[B][COLOR=#008000]'-------------------------------------------------------------------------------------------------------------[/COLOR][/B]
[B][COLOR=#008000]' Code below is shared by all TextBoxes.
[/COLOR][/B]
Private Sub ShowMenu(ByVal TextBox As Object, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim oDataObj As New DataObject, sClipText As String
If TextBox.MultiLine = False Then TextBox.MultiLine = True
If Button = 2 Then
Call oDataObj.GetFromClipboard
On Error Resume Next
sClipText = oDataObj.GetText(1)
On Error GoTo 0
If Len(sClipText) Then
TextBox.SelStart = Len(TextBox.Text) + 1
Call CreateRghtClickMenu(TextBox, True)
Else
Call CreateRghtClickMenu(TextBox, False)
End If
Call CommandBars("PasteMenu").ShowPopup
Call DeleteRghtClickMenu
End If
End Sub
Private Sub PasteMacro(ByVal TextBoxName As String)
Me.OLEObjects(TextBoxName).Object.Paste
End Sub
Private Sub CreateRghtClickMenu(ByVal TextBox As Object, ByVal Enabled As Boolean)
Dim objCmb As CommandBar
Call DeleteRghtClickMenu
Set objCmb = Application.CommandBars.Add(Position:=msoBarPopup, Temporary:=True)
With objCmb
objCmb.Name = "PasteMenu"
With .Controls.Add(msoControlButton)
.Caption = IIf(Enabled, "Paste", "ClipBoard Empty !")
.FaceId = 22
.Enabled = Enabled
.OnAction = "'" & Me.CodeName & ".PasteMacro """ & TextBox.Name & "'"
End With
End With
End Sub
Private Sub DeleteRghtClickMenu()
On Error Resume Next
CommandBars("PasteMenu").Delete
End Sub