'***********************************************
'Menu contextuel "couper/copier/coller" sur textbox dans userform
'patricktoulon
'menu change sur la base de la version combobox de @job75
'*************************************************************
Option Explicit
Dim ctrl As Object
Sub createmenu(ctl As Object)
Dim barre, arrbutton, I%: delebar: Set ctrl = ctl
arrbutton = Array("Couper", "Copier", "Coller")
Set barre = Application.CommandBars.Add("CopierColler", msoBarPopup, False, True)
For I = 0 To UBound(arrbutton)
With barre.Controls.Add(msoControlButton, 1, , , True)
.Caption = arrbutton(I): .Tag = I: .OnAction = "Menu_change"
If .Caption = "Couper" Then If ctl.SelLength = 0 Or TypeName(ctl) = "ComboBox" Then .Enabled = False
If .Caption = "Copier" And ctl.Value = "" Then .Enabled = False
If TypeName(ctl) = "ComboBox" And .Caption = "Coller" Then .Enabled = False
End With
Next
barre.ShowPopup
End Sub
Sub delebar(): On Error Resume Next: CommandBars("CopierColler").Delete: End Sub
Private Sub menu_Change()
Dim o As Object, Index%
Index = Val(Application.CommandBars.ActionControl.Tag)
With ctrl
.SetFocus
Set o = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") ' DataObject en late binding
If Index < 2 Then o.SetText .SelText: o.PutInClipboard 'couper/copier on met la sélection de texte dans le clipboard
Select Case Index ' remplacement des if par un select case
Case 0: .SelText = ""
Case 2: On Error Resume Next: o.GetFromClipboard: If o.GetText <> "" Then .SelText = o.GetText 'ajout du controle(si clip est vide )
End Select
End With
End Sub