Clipboard Function

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
Is there is anyway (i m sure, there is) to copy the text of Textbox to Clipboard.. ? please let me know
 

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.
L

Legacy 98055

Guest
Well... I thought the Copy method did this but it is not working for me. Anybody know why?

TextBox1.Copy

is supposed to copy the contents of TextBox1 to the clipboard. The Cut method is not working as well...
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows
TextBox1.Copy will copy the whole actual Control Object not its Text .

one way is to use the DataObject Class which requires a reference to the MsForms library before it can be used.

here is function that will do the job :

Code:
Option Explicit

Function CopyTextBox_Text(oTextBox As MSForms.TextBox) As Boolean

    Dim oData As DataObject
    Set oData = New DataObject
    oData.Clear
    If Len(oTextBox.Text) <> 0 Then
        oData.SetText oTextBox.Text
        oData.PutInClipboard
        CopyTextBox_Text = True
    End If

End Function

and here is test routine that pastes the contents of TextBox1 onto Cell A1 :

Code:
Sub test()

    If CopyTextBox_Text(Me.TextBox1) Then
        ActiveSheet.Paste Range("a1")
    Else
        MsgBox "No text to paste !", vbExclamation
    End If

End Sub


Regards.
 
L

Legacy 98055

Guest
Not so. After reading the help file more carefully, I saw that it copies the selected text of the textbox control.

This being the case, you need to select the text:

TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
TextBox1.Copy
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Not so. After reading the help file more carefully, I saw that it copies the selected text of the textbox control.

This being the case, you need to select the text:

TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
TextBox1.Copy

Yes, you can achieve the same result with this code but will need to ativate the TextBox first . As I said the code I posted is just one way of solving the problem not the only one.

Regards.
 
L

Legacy 98055

Guest
Yes, you can achieve the same result with this code but will need to ativate the TextBox first

You don't have to activate anything.

Jaafar. I'm trying to establish the correct use for the Copy and Cut methods. That's all. You keep posting wrong information.

TextBox1.Copy will copy the whole actual Control Object not its Text .
need to ativate the TextBox first

Please check the help file more closely. I would not have had this question to begin with if I would have read more carefully to begin with.
 

Gary Drumm

Active Member
Joined
Feb 22, 2005
Messages
462

ADVERTISEMENT

I downloaded a new program called Note Tab Lite, from Notetab.com.
It has a lot of features that I haven't really explored yet, but might be worth a look. I found the link on this board.

Have you tried Select, Edit, Copy?
Or perhaps Alt / Print screen?
Both of those copy to the clipboard.
 

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
Well... what should i say... i am more confused then i was before... cuz neither of codes worked for me... I am still scratching my head and seeking the info thru Google... I have to say that i really didn't thought that copying the text to windows clipboard will be such a difficult task... anyways Pals keep me posted if you come to know anything
 
L

Legacy 98055

Guest
The Textbox control provide three methods to work with the clipboard. Copy, Cut, and Paste. They all work if used properly. Download example.

1131422.zip
 

irresistible007

Board Regular
Joined
Nov 24, 2005
Messages
173
This topic is getting more interesting then i ever thought about it....

I simply shortened your code as per my requirement:

Code:
Private Sub CommandButton1_Click()
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
    TextBox1.Copy
End Sub

It worked fine when i placed the Text Box and command Button on a User Form, But to my Surprise, when i kicked in the same controls on Excel sheet (instead of Form) and pasted the same code... it actually copied the control itself....

Hey anybody there to resolve this **** prob?
 

Forum statistics

Threads
1,137,205
Messages
5,680,174
Members
419,887
Latest member
Vasokir

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
Top