Consulting

Results 1 to 2 of 2

Thread: Get (some of) Excel Clipboard contents via Windows clipboard "XML Spreadsheet" format

  1. #1
    VBAX Tutor TheAntiGates's Avatar
    Joined
    Feb 2005
    Location
    Tejas
    Posts
    263
    Location

    Get (some of) Excel Clipboard contents via Windows clipboard "XML Spreadsheet" format

    If you can tell me whether this is useful or wasteful, I'm swimming above my pay grade here so straighten me out. After some experimenting I determined that you can (only partially) "simulate" Excel's copy buffer via the Windows Clipboard using its "XML Spreadsheet" format thereof. This is wildly limited so I'm not going to receive a Congressional Medal of Honor, but maybe it might help or inspire someone usefully.

    Before going any further, this may be bombing an anthill. Maybe there's a vastly simpler way to get the clipboard contents, such as with DataObj. Take this FWIW.

    I modified Leigh Webber's sometimes popularly cited code (link below) for retrieving and restoring Windows clipboard's Rich Text content (such as you might have if you copied a multi-colored passage in Word), via API calls, modified to instead use the XML Spreadsheet format. The result is that I was able to
    - Select a rectangle of cells on a worksheet, and Ctrl-C.
    - Go off somewhere else - to another sheet, then another program.
    - Run code to store the XML representation of Windows clipboard in a string variable.
    - Copy some random Notepad text into the Windows copy buffer just to keep me honest (wiping out the clipboard).
    - Restore the Windows copy buffer from my string.
    - Hit Control-V on a different Workbook (or even a different instance),
    and I got all the contents copied, and basic formatting, including pattern and borders.
    I did NOT pick up conditional formatting. I did not even pick up formulae.

    Maybe someone could confirm this but it appears that all I essentially achieved was to pick up exactly what gets transferred when you open two instances of Excel (the same version, or different versions) and copy/paste a range from one to the other. Values but no formulae get copied when you do that, for example.

    This is not that big a deal. However if someone has an application where they could benefit from the limited "XML" content accessible from the Windows clipboard, this is one way. One might think, well, they could just save off the Selection range, and they'd have EVERYTHING that an Excel copy and paste gets (like, the formulae, duh!). I suppose so. Yet perhaps there's some scenario where
    - cells had been copied into the windows clipboard,
    - they're no longer in the Office clipboard,
    - they're no longer selected, and
    - just the values and maybe basic format are desired. (and the cell sequence and row/column orientation)
    In that scenario, it's really tough (if not impossible?) to programatically know "what" has been copied, so this would query it from the Windows clipboard.

    Perhaps in that or another scenario where multiple instances of Excel are running this could be useful. Or I could be just high in this approach. (For one thing, I don't know exactly how much of this can be done with DataObject.) If you can tell me whether this idea is useful or wasteful, I'm swimming above my pay grade here so straighten me out.

    Webber article
    A clipboard object for VBA, including Microsoft Word
    https://social.msdn.microsoft.com/Fo...?forum=worddev

    Simplified (text) version of the process:
    Retrieve Information from the Clipboard
    https://msdn.microsoft.com/en-us/vba...-the-clipboard

    Chip, on DataObject:
    Using The Clipboard In VBA
    http://www.cpearson.com/excel/clipboard.aspx
    I just found a cool semi-advanced VBA page - dictionary, queue, etc. http://analystcave.com/excel-vba-dic...ta-structures/

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A bit of googling came up with this, which may be of interest in showing what is possible.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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