Search code examples
excelvbaoffice36564-bit

How to fix userform Textbox copy to clipboard no longer working


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.


Solution

  • 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