copy a textbox in excel to the clipboard

rbsuttn

New Member
Joined
Aug 5, 2017
Messages
5
Good morning
i've spent the last few evenings reading and trying different examples (dozens) of how to copy the information in a textbox to the clipboard in excel. I've gotten close on some of the examples while others give me the infamous two boxes or two question marks

I have a command button in a worksheet named Injects, there is a text box "textbox111" want to to click the button and have the info from this text box stored in the clipboard

Private Sub CommandButton2_Click()
Dim strbox As String
strbox = TextBox111
Range(TextBox111).Copy
End Sub

i have had no luck, any help would be much appreciated
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This will put the contents of a textbox in the clipboard.
VBA Code:
Dim doObj As New DataObject

With doObj
    .SetText TextBox111.Text
    .PutInClipboard
End With
 
Upvote 0
This will put the contents of a textbox in the clipboard.
VBA Code:
Dim doObj As New DataObject

With doObj
    .SetText TextBox111.Text
    .PutInClipboard
End With
Mikerickson - thanks just tried it and i get the two "question marks in two squares" when i paste it. i've read a few other threads that stated there is a bug in the code but i can't seem to follow the threads on how to circumvent the bug.
 
Upvote 0
You could add this function:

Code:
Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function

then use:

Code:
SetClipBoardText TextBox111.Text

wherever you need to put the text in the clipboard.
 
Upvote 0
You could add this function:

Code:
Function SetClipBoardText(ByVal Text As Variant) As Boolean
    SetClipBoardText = CreateObject("htmlfile").ParentWindow.ClipboardData.SetData("Text", Text)
End Function

then use:

Code:
SetClipBoardText TextBox111.Text

wherever you need to put the text in the clipboard.
Rory, Thanks a million!!! worked like a charm. Now i'll try and spend some time to try and figure out why it works but this is a win!!!
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top