+ Reply to Thread
Results 1 to 5 of 5

Thread: How to copy the selected text in a cell to the clipboard with VBA

  1. #1
    Registered User
    Join Date
    12-02-2010
    Location
    Huntington, New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    20

    How to copy the selected text in a cell to the clipboard with VBA

    Hi All;

    For reasons relating to my efforts to write VBA code to paste an HTML table from a variable to a worksheet, I need to copy the text within a cell (but not the entire cell) to the clipboard. I find that if I do that manually using <CTRL> C and use code such as:

     Activesheet.paste :=Destination = Range("A5")
    The HTML table gets pasted as a table with each cell in its proper worksheet cell. Copying and pasting the entire cell, only gets the HTML text reproduced.

    So I have tried all I can think of to copy the HTML text to the clipboard. Here are some of my futile attempts, after having selected the text which I wish to copy from within the cell using:
    Range("A1").activate  'that's where the text is
    sendkeys "{F2}"  'places the insertion point at the end of the text in the cell
    sendkeys "+{UP 500} 'there are CRLF characters in the text - continued Up arrows gets past them.
    sendkeys "+{HOME"} - gets the selection to the beginning of the last line of text
    Then, trying to copy the selected text ...
    • Using Sendkeys to emulate <CTRL> C - nothing happens, no copying
    • Using Range("A1").copy in several variations - "call rejected by callee" error message
    • Cant easily call and work with the clipboard because Office 2007 has hidden the Forms2_0.dll reference (which used to contain the Clipboard Object Library) where I can't find it.
    • Tried Selection.copy - same "call rejected by callee" error message as above

    Any help with a new approach (or a working solution ) would be appreciated.
    Thanks,

    Stosh
    Last edited by srschiller; 12-16-2010 at 09:54 AM.

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: How to copy the selected text in a cell to the clipboard with VBA

    Mayhap it would be a good idea if you uploaded a mock workbook with the HTML table for the forum to look at. That way it will be easier to understand what exactly you want the code to do.
    Please leave a message after the beep!

  3. #3
    Registered User
    Join Date
    12-02-2010
    Location
    Huntington, New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: How to copy the selected text in a cell to the clipboard with VBA

    Thanks, I'll put that together along with the code to date.

    Stosh

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: How to copy the selected text in a cell to the clipboard with VBA

    You might try a dataobject - something like:
    Dim objdata as msforms.dataobject
    Set objdata = new msforms.dataobject
    Objdata.settext activecell.value
    Objdata.putinclipboard
    To set the forms reference easily, just insert a userform. You can late bind it, but I can't remember the Guid offhand and I'm on an iPad currentl.

  5. #5
    Registered User
    Join Date
    12-02-2010
    Location
    Huntington, New York, USA
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: How to copy the selected text in a cell to the clipboard with VBA

    This works a treat and was exactly what I was looking for. Thank you, Romperstomper, for your knowledge and willingness to help. I am marking this thread "SOLVED" and passing along my addition to your reputation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0