0

For the past few months I have had no issue with this script

Private Sub CommandButton3_Click()
    With New MSForms.DataObject
        .SetText TextBox2.Text
        .PutInClipboard
    End With
End Sub 

It has functioned perfectly, copying line breaks and text with no issue.

Today when using this same code, my paste output is:

��

However, I am able to manually select the text in the userform and press crtl+c and my paste output is exactly as it should be.

I have never had this issue before today. I've tried restarting, and clearing my worksheet, but it doesn't work. I've tried adding a few printouts throughout the sub, but I'm not sure what I should be looking for since there are only a few lines and everything looks right for me.

I'm using a multipage userform with two textboxes. I've confirmed there isn't an issue with the code referring to the wrong textbox, and that the error is occurring with both copy on click subs.

Mannimar
  • 17
  • 6
  • 1
    That `DataObject` approach seems unreliable lately. If you're using Windows you can use Win API calls: https://stackoverflow.com/questions/66282167/run-array-formula-based-on-selected-cell-range-and-copy-results-to-clipboard/66282746#66282746 – Tim Williams Apr 27 '21 at 23:40
  • Works just fine for me.. **1.** What do you have in the textbox? I mean did you type something directly or pasted something from somewhere? **2.** Where are you pasting the data? **3.** Is there any other code which might be interferring with the clipboard? – Siddharth Rout Apr 28 '21 at 03:46
  • Siddharth, maybe it's the bug that affects SetText? If so, the output would be 2 question marks (??), instead of whatever TextBox2.Text is. – Andrew Apr 28 '21 at 07:45
  • @Andrew Ah yes. https://social.msdn.microsoft.com/Forums/windowsserver/en-US/48e8c30c-24ee-458e-a873-a4e6e13f5926/dataobject-settext-and-putinclipboard-sequence-puts-invalid-data-hex-63-characters-in-clipboard – Siddharth Rout Apr 28 '21 at 07:59
  • That's the one. Sad that it's still an issue in 2021. – Andrew Apr 28 '21 at 08:02
  • @SiddharthRout It's just plain text pasted to a webpage input which is the'??'. However, when pasted to notepad it's just blank. The text is just along the lines of Person: John Amount: $1000 Source: Mail/Web/Ect on different lines. – Mannimar Apr 28 '21 at 13:18
  • @TimWilliams I used a modified version of that for 64-Bit, and it seems to work for now. If you post an answer I'll give you the credit you're due! – Mannimar Apr 28 '21 at 13:40

1 Answers1

1

The DataObject approach seems unreliable lately.

If you're using Windows you can use Win API calls:

Run array formula based on selected cell range and copy results to clipboard

Also - including as related: Injecting RTF code in the Clipboard to paste into MS Word as RTF text via a VBA macro

Tim Williams
  • 154,628
  • 8
  • 97
  • 125