0

I'm currently looking for a methode to copy a 1D array to clipboard by using excel vba. So far, I already fail by copying only one string to the clipboard. What I did is:

First: I included the "Microsoft Forms 2.0 Object Library" to my project

Second: I tried to copy the strText string to the clipboard by using the following code.

Sub copyToClipboard()
   Dim objData As New MSForms.DataObject       
   strText= "1"
   objData.SetText strText
   objData.PutInClipboard

   objData.GetFromClipboard
   resultString = objData.GetText
End Sub

If I paste the clipboard to a text Editor I get some funny symbols. If I check the content of resultString, I get "??".


EDIT: As mentionded below, I only get the "??" if I debug the code. A normal run is giving the expected output. Here is an image of the output windowThe second output line is while debuging the code, the first one is a normal run

The second output line is while debuging the code, the first one is a normal run.

2
  • 1
    What is objData? Commented May 15, 2017 at 13:00
  • Sorry I forget to copy one line. Dim objData As New MSForms.DataObject Commented May 16, 2017 at 5:55

2 Answers 2

5

I wasn't able to find "Microsoft Forms 2.0 Object Library", but it's also possible to create a UserForm inside your Workbook (The UserForm can be ignored then). Then use this code:

Dim objData As New MSForms.DataObject

'Load Test to Clipboard
objData.SetText "TEST"
objData.PutInClipboard

'Get Text from Clipboard
objData.GetFromClipboard
Debug.Print objData.GetText

Then, to load an Array to the Clipboard, use something like this:

Dim objData As New MSForms.DataObject
'Join combines all Array items to one string with the specified delimiter
objData.SetText Join(ARRAY, ",") 'ARRAY is your Array-Variable, "," is your delimiter
objData.PutInClipboard
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, but unfortunatly your code gave the same output as my code befor. Output window shows "??" :-/ To make it more curio, I figured out, that the code only print "??" if i run it in debug mode. In normal mode I get the "TEST". Why?
@Stefan this is really weird
0

Maybe you run into what is described here DataObject .setText and .putInClipboard sequence puts invalid data (Hex 63 characters) in clipboard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.