Copy text box contents

J

John Pierce

I would like to have my program copy the contents of a text box on a
form to the clipboard as soon as it is filled and exited. The result
would be the same as if I went back and double-clicked on the text box
and pressed Ctrl+C so it can be pasted into another program.
 
J

JLGWhiz

I put a text box on a form. Loaded the TextBox1 using the form initialize
event. Then set focus to select the current value of the text, and then
copied the TextBox.
It put the text on the clip board and I could then pick a cell to paste the
text to. Here is the code behind the Userform.

Private Sub UserForm_Click()
TextBox1.SetFocus
TextBox1.Copy
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Text = Range("B3").Value
End Sub
 
J

John Pierce

I put a text box on a form.  Loaded the TextBox1 using the form initialize
event.  Then set focus to select the current value of the text, and then
copied the TextBox.
It put the text on the clip board and I could then pick a cell to paste the
text to.  Here is the code behind the Userform.

Private Sub UserForm_Click()
    TextBox1.SetFocus
    TextBox1.Copy
    Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
    Me.TextBox1.Text = Range("B3").Value
End Sub
This isn't working for me. I believe the true test of what I'm trying
to do would be to create a form
with two text boxes. Type something in the first text box then press
tab to go to the next text box,
press Crtl+V and paste what was just typed in the first box. Of
course, I don't know if it can be done,
but it seems that an Exit or Afterupdate event might do it.
 
R

Rick Rothstein \(MVP - VB\)

Try it this way...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.Copy
End With
End Sub

Whenever focus moves from TextBox1 to any other control OR if the UserForm
is closed (thus effectively exiting the TextBox), the contents of TextBox1
will be placed in the Clipboard... erasing anything that is already in the
Clipboard, of course. This makes it a "dangerous" functionality to
implement; for example, what if your user copied something into the
Clipboard from a parallel running program, closed that program, came back to
your program and exited that TextBox, closed or left your program and went
to paste what he/she thought was in the Clipboard?

Rick


I put a text box on a form. Loaded the TextBox1 using the form initialize
event. Then set focus to select the current value of the text, and then
copied the TextBox.
It put the text on the clip board and I could then pick a cell to paste
the
text to. Here is the code behind the Userform.

Private Sub UserForm_Click()
TextBox1.SetFocus
TextBox1.Copy
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Me.TextBox1.Text = Range("B3").Value
End Sub
This isn't working for me. I believe the true test of what I'm trying
to do would be to create a form
with two text boxes. Type something in the first text box then press
tab to go to the next text box,
press Crtl+V and paste what was just typed in the first box. Of
course, I don't know if it can be done,
but it seems that an Exit or Afterupdate event might do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top