Copy/Paste buttons to copy and paste only highlighted Text in a Userform textbox to the clipboard

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
Office Version
  1. 365
Platform
  1. Windows
As title indicates. It basically replaces Ctrl+C and Ctrl+V.
This code copies the entire textbox contents. Don't want that - just a manually highlighted string within the textbox text.
Code:
Private Sub CommandButton1_Click()
Dim strclipboard As String
Set objData = New DataObject
strclipboard = TextBox1.Text
objData.SetText strclipboard
objData.PutInClipBoard
objData.GetFromClipboard
strclipboard = ""
strclipboard = objDataSetText
End Sub

As mentioned, the code copies the entire "The quick brown fox ran up a tree"
How would the correct code only copy "ran up a tree" highlighted or "brown fox" highlighted
and paste in any textbox or document with a Paste button. or by right clicking ?

Thanks for anyone's help.
cr






COPIES ENTIRE TEXT.jpg
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
The quick brown fox ran up a tree
SelStart property gives you the starting point of the selected text (so 20?). SelLength gives you the number of characters selected (13).
Mid("The quick brown fox ran up a tree", 20,13) will return the sub string.
I have to wonder why anyone would code for this when there is ctrl+c & ctrl+v :unsure:

Then again, I had no idea that foxes could climb trees, so always learning. :biggrin:
 
Upvote 0
I paid more attention to your code after that post. If you're just going to paste into a sheet or userform textbox you don't need the clipboard code, no?
And if that is really your code, it doesn't work, correct? I'd wager that you don't use Option Explicit because you don't declare variables, and this
objDataSetText

would be regarded as a variable.
 
Last edited:
Upvote 0
Hi Micron - I'm trying to understand your reply. Bear with me. No to overkill this but - assigning preset Selstart and Seltext numbers limits
the highlighted copy range - all I want to do is just highlight and copy any highlighted text in a userform textbox - be it the entire textbox text
or a portion of the textbox text - any highlighted portion - grab that highlighted text, copy it to the clipboard, place the
cursor in a textbox on another userform, and use another Paste button( getClipboard selection and Paste selection ?)
to paste that selection of text in the other userform textbox.

Excel has to be able to recognize that it needs to select and copy any highlighted text from the beginning of the blue highlighted
position to the end of the blue highlighted position - and that will vary. To me, this should be beyond simple. I just can't figure it out -

Thanks again for trying to help me as you've done in the past.
cr
 
Upvote 0
I think I already understood, so last night I entered text in a userform textbox, highlighted a portion and ran my code and raised a message box with only the highlighted portion. Not sure I saved it, so I'll look and be back ASAP.
 
Upvote 0
Fri 3/3/23 9:43 AM CST:
Thanks a mil Micron - I'll look forward to receiving it

cr
 
Upvote 0
This seems to work for me (I've never used the object before) so this may be lacking in terms of error prevention (such as nothing having been selected when button clicked). I recommend adding that if it seems to work for you.
VBA Code:
Private Sub CommandButton1_Click()
Dim strclipboard As String
Dim objData As Object

Set objData = New DataObject
strclipboard = Mid(Me.TextBox1, Me.TextBox1.SelStart, Me.TextBox1.SelLength)
objData.SetText strclipboard
objData.PutInClipboard
MsgBox objData.GetText(1)

'strclipboard = ""
'strclipboard = objData.SetText
End Sub
Here's my reference
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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