We help IT Professionals succeed at work.

copy to clipboard vba

Wilder1626
Wilder1626 asked
on
467 Views
Last Modified: 2012-02-08
Hello all

I would like to know if there is a way in VBA excel to double click in the textbox1 and it would copy the full content of it?

I'm able to do it in VB6 but not in VBA

I tried that way but not working

 Textbox1.SelStart = 0
    carrier_id.SelLength = Len(Textbox1.Text)
Clipboard.Clear
Clipboard.SetText Textbox1.Text

Open in new window


Thanks again for your help
Comment
Watch Question

Alfredo Luis Torres SerranoASP .Net Developer

Commented:
Try this:

Code:

Sub PutStringInClipboard()
'Sub routine to put a string in the clipboard

Dim ans As String
Dim ansDataO As DataObject
Set ansDataO = New DataObject

ans = "Some Text"

ansDataO.SetText ans
ansDataO.PutInClipboard
   
End Sub
CERTIFIED EXPERT

Author

Commented:
Small detail, can it select the text at the same time? Not just copy to clipboard?
Alfredo Luis Torres SerranoASP .Net Developer

Commented:
yes with somethng like

With TextBox1
      .Text = Range("A1").Value
      .SelStart = 5
      .SelLength = 6
       strPal = .SelText
       MsgBox "The b Seletec Word is: " & strPal
.SetFocus
End With
CERTIFIED EXPERT

Author

Commented:
Something is strange.

Im removing this part of the code: MsgBox "The b Seletec Word is: " & strPal

Because i dont want to msgbox. But when i remove it, it does not select the textbox content anymore.
Alfredo Luis Torres SerranoASP .Net Developer

Commented:
This should work
With TextBox1
      .Text = Range("A1").Value
      .SelStart = 5
      .SelLength = 6
       strPal = .SelText
       .SetFocus
End With
CERTIFIED EXPERT

Author

Commented:
I tried that why but it was not working

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim ans As String
Dim ansDataO As DataObject
Set ansDataO = New DataObject

ans = TextBox1.Text

ansDataO.SetText ans
ansDataO.PutInClipboard


With TextBox1
      .Text = TextBox1.Text
      .SelStart = 0
      .SelLength = 12
       strPal = .SelText
       .SetFocus
End With


End Sub

Open in new window

Alfredo Luis Torres SerranoASP .Net Developer

Commented:
when and where are you calling this macro ??

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
CERTIFIED EXPERT

Author

Commented:
I double click in the Texbox1 directly and the code is set in:

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
ASP .Net Developer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
I have the same result where it does not select the text.

But here's the thing:

If i do it this way it work great:
Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim ans As String
Dim ansDataO As DataObject
Set ansDataO = New DataObject

ans = TextBox1.Text

ansDataO.SetText ans
ansDataO.PutInClipboard


With TextBox1
      .Text = TextBox1.Text
      .SelStart = 0
      .SelLength = 12
       strPal = .SelText
       .SetFocus
End With
End Sub

Open in new window

Alfredo Luis Torres SerranoASP .Net Developer

Commented:
then the problem is with the  handler signature
CERTIFIED EXPERT

Author

Commented:
Thank you so much for your help.

It's now working great.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.