Private Sub CommandButton1_Click()
Dim strclipboard As String
Dim objData As Object
Set objData = New DataObject
strclipboard = Mid(Me.TextBox1, Me.TextBox1.SelStart, Me.TextBox1.SelLength)
objData.SetText strclipboard
objData.PutInClipboard
MsgBox objData.GetText(1)
End Sub
Did not work for me. I don’t understand it.This is all I have that might be of some use to you. It puts the text selected in a userform textbox into the clipboard, then gets it from the clipboard and presents it in a message box. The key parts for you would be the dataobject parts.
VBA Code:Private Sub CommandButton1_Click() Dim strclipboard As String Dim objData As Object Set objData = New DataObject strclipboard = Mid(Me.TextBox1, Me.TextBox1.SelStart, Me.TextBox1.SelLength) objData.SetText strclipboard objData.PutInClipboard MsgBox objData.GetText(1) End Sub
Range("A1").PasteSpecial Paste:=xlPasteAll
@Micron and @Alex Blakenburg I tried everything, including codes both of you provided, but nothing was working consistently. Finally, I added a step in the start to activate excel and clear cell A1 contents before doing anything else. Now it works consistently. Thank you both for your help.If you are manually copying something from another application and using the macro to paste it try:
VBA Code:Range("A1").PasteSpecial Paste:=xlPasteAll