• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Select and copy whole text of a TextBox to clipboard

Pete Wright

Member
Hello everybody!

I have a problem with Excel VBA code, that should select and copy the text of a TextBox to clipboard.

As you can see on the screenshot below I have a workbook with two sheets.
The first sheet contains the generated data and the second sheet contains a TextBox which is filled with the data via VBA.
The buttons which "create" and "clear" the data work just fine. The problem is with the "copy" button.
I named the sheet "script" and the TextBox "output"
Now I want to select & copy the text with a button.

I already searched the internet and other forums, but nothing I found worked as expected.

Code:
    With script.output            'script = Worksheet, output = TextBox
        .SelStart = 0
        .SelLength = Len(.Text)
    End With

What am I doing wrong? Does anyone have a solution?

Kind regards
Pete Wright





59062
 

Attachments

  • acad.xlsm
    40.1 KB · Views: 5
This will copy it to variable OutVal

Code:
Dim OutVal As String
OutVal = script.output.Text

Many Thanks! Now that the text is stored in a variable, I can do something with it.
Besides, I have just found this article: VBA to Clipboard (cpearson.com)

Code:
Dim DataObj As New MSForms.DataObject
DataObj.SetText OutVal
DataObj.PutInClipboard

With both code snippets combined I finally get what I wanted.


But I have a few more questions now to expand my knowledge:

  1. does your code work all the time or are there some restrictions?
  2. Do I need to create a new MSForms.DataObject in order to copy a variable into clipboard or is there another way?
  3. Why does my code not work and what is the correct use of the SelStart, SelLength, SelText properties?
    Code:
    script.output.SelStart = 0
    script.output.SelLength = Len(script.output)
    script.output.SelText
 
1) guess only trial and error will tell
2) if you want to copy it to clipboard then yup gotta use the code you wrote. also below link gives more info
[/URL]
3) refer to below
[/URL]

Many Thanks!

But I still don't understand, why my code doesn't select anything.
I followed the rules of the MS Docs, but it doesn't work.

59079

Can you tell me why?
 
what are you trying to do with the code? because it looks like you're purely selecting everything
 
Just FYI, the DataObject is quite buggy on Windows 8 and later. If you have a File Explorer window open, you can end up with just two box characters on the clipboard instead of your desired text. API calls are safer.
 
Back
Top