Copy Textbox Text To Clipboard

  • Hi,


    I've tried to adapt Aaron Blood's method of sending the text from a textbox to the clipboard but the code falls over in the middle line, possibly because my textbox1 is from the drawing toolbar:


    Code
    Sub TextCopy() 
        Application.SendKeys ("^c~") 
        InputBox "clipboard", ,  Sheets("Sheet1").TextBox1.Text 
    End Sub


    If anyone knows how to adapt the above code or knows another method any help is much appreciated. Aaron's avatar is good fun too! Thanks in advance, Upside

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    No sorry - it still fails using your substitute.

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    I tested it like this and worked fine


    Code
    Sub Test()
    MsgBox Sheets("Sheet1").Shapes("Text Box 1").TextFrame.Characters.Text
    End Sub


    VBA Noob

  • Re: Copy Textbox Text To Clipboard


    Sorry VBANoob - my object was called textbox1 instead of Text Box 1. It now works - thanks. Final solution and code enclosed:


    Code
    Application.SendKeys ("^c~")
         InputBox "clipboard", , Sheets("Sheet1").Shapes("textbox1").TextFrame.Characters.Text

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    This uses the DataObject data type to put the text from a Drawing Textbox into the clipboard.


    Code
    Dim myClipboard As New DataObject
    
    
    myClipboard.SetText Shapes("TextBox 1").TextFrame.Characters.Text
    
    
    myClipboard.PutInClipboard
  • Re: Copy Textbox Text To Clipboard


    A problem with both solutions .... it appears that only the first 255 characters in the textbox are passed to the clipboard. My report has considerably more characters in it than that.

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    The question is where would be a suitable 'receptacle' ? I guess it could be ported into an open word document but I think that is venturing toward another thread / forum.

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    Try using this;the clipboard createdas/in class module



    Class Module



    regards pike

  • Re: Copy Textbox Text To Clipboard


    I am venturing into unknown waters here in terms of where to paste the code but I'm reluctant to not see this project through after all the time invested. Can someone place the code in the right place for me please it should be attached to the button on the sheet?


    Thanks everyone for your contributions, I really appreciate it.

  • Re: Copy Textbox Text To Clipboard


    The DataObject will hold more than 255 characters.
    The following code demonstrates that there is no 255 limit. (tested on Windows XP, Excel2007)


  • Re: Copy Textbox Text To Clipboard


    The DataObject can handle huge strings, Mike -- theoretically 2 GB, and I've moved megabytes to and from routinely.


    It's moving more than 255 characters at a clip from or to a shape, including a textbox. Your code doesn't work in Excel 2003 with more than that.

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Copy Textbox Text To Clipboard


    Oh, good to know. (I found that my (home) Excel2004 doesn't move more than 255 characters in or out of a text box either.)


    I looked at that file and found ActiveX controls (which aren't supported for Mac).
    So, I added these routines and linked it to a Forms button. Its untested on this file (can't leave design mode, since it can't create Button1), but it tested well with Forms Menu controls. (I'm not sure of the syntax for getting the text from an ActiveX text box, but if that is an issue, I can figure out the syntax tommorow, when I get to a Windows computer.)


  • Re: Copy Textbox Text To Clipboard


    Thank you - I got it to work!! One tiny problem is that if the code executes while the text box is empty it throws an error.

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    Error: DataObject:PutinClipboard Not Implemented


    It falls down at Line 18:


    myClipboard.PutInClipboard

    I am new to VBA - comments on how to improve my code are always welcome.

  • Re: Copy Textbox Text To Clipboard


    Putting that inside the If...EndIf should fix that.


  • Re: Copy Textbox Text To Clipboard


    Hi Mike - sorry but this code still fails to stop the above error at the same line.[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]I did some fumbling about and this seems to stop it from crashing if the text box is empty. Thanks Mike for your time - you should get an MVP nomination.


    Best wishes, Upside


    I am new to VBA - comments on how to improve my code are always welcome.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!