Non-userform text box: How to copy from clipboard and paste into it

daneyuleb

New Member
Joined
Dec 8, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Most every post that shows how to copy or paste text from a textbox assumes a userform text box. Trying those with a non-user form textbox--one that was just inserted directly on to the sheet--doesn't work and gives method not found errors typically. I've wasted a lot of time chasing those only find out the author was assuming the textbox is part of a userform.

Can someone please tell how to simply copy text from an inserted textbox into the clipboard via vba? And then tell how to go about pasting text into an inserted textbox from the clipboard?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
You really do not need to copy it.
Try this example:
VBA Code:
Private Sub CommandButton1_Click()
'Modified  12/9/2021  12:27:41 AM  EST
Sheets(1).TextBox2.Value = Sheets(1).TextBox1.Value
End Sub
 
Upvote 0
Thanks for the response! Sorry if I was confusing, but in my case, I do need to copy it to the clipboard. And paste from the clipboard. But I can see how my description made it look like I was trying to go from one textbox to another.

Just one textbox that has to be able to dump all it's contents to the clipboard (for another application to use). Then, that same textbox has to be able to get a paste from the clipboard. Easy if the textbox is part of a userform, but not so much for an inserted one evidently!
 
Upvote 0
@daneyuleb, Welcome to MrExcel
To work with clipboard you need to add the reference “Microsoft Forms 2.0 Object Library” (in VBE menu select Tool>References> select “Microsoft Forms 2.0 Object Library”)
Ty this:
This will copy textbox1 text to clipboard
VBA Code:
Sub copyIt()
Dim obj As New DataObject
    
    obj.SetText TextBox1.Text
    obj.PutInClipboard

End Sub

this will paste clipboard to textbox2

VBA Code:
Sub pasteIt()
Dim obj As New DataObject

obj.GetFromClipboard
TextBox2.Text = obj.GetText

End Sub
 
Upvote 0
Thanks for that!
When I try the: TextBox2.Text = obj.GetText, I get a: "Method or Data Member not found". And I get it as well if I precede it with the sheet name.

Can a text box that's not in a userform be accessed that way? I thought it needed the shapes("textbox name") syntax. But when I do that... I don't see a "text" method to use. :(
 
Upvote 0
Thanks for that!
When I try the: TextBox2.Text = obj.GetText, I get a: "Method or Data Member not found". And I get it as well if I precede it with the sheet name.

Can a text box that's not in a userform be accessed that way? I thought it needed the shapes("textbox name") syntax. But when I do that... I don't see a "text" method to use. :(

Forgot to add-- “Microsoft Forms 2.0 Object Library” IS selected and active as a Reference.
 
Upvote 0
1. The textbox are activex control textbox in a sheet.
2. The code should be placed in the sheet code module.
 
Upvote 0
I thought it needed the shapes("textbox name") syntax.
Sorry, I just realized that you're talking about Shape object.
Try:
VBA Code:
Sub copyIt()
Dim obj As New DataObject
   
    obj.SetText Sheets("Sheet1").Shapes("Textbox 1").TextFrame.Characters.Text
    obj.PutInClipboard

End Sub

VBA Code:
Sub pasteIt()
Dim obj As New DataObject

obj.GetFromClipboard
Sheets("Sheet1").Shapes("Textbox 2").TextFrame.Characters.Text = obj.GetText

End Sub
 
Upvote 0
Oh my gosh! That was it!

I had tried the "Shapes("Textbox 2") construction, but didn't know to go into the TextFrame.Characters.Text method!!

Thank you so much Akuini!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

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