Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Warrington, Cheshire
    Posts
    355
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Copying datagrid to clipboard

    Can anyone please suggest a method for copying a dataset or datagrid to the windows clipboard?
    I need to do that so that i can paste the whole dataset to a new Excel workbook.
    The following code is the original but presents errors on the oBook:
    <pre>Sub btnExport_onclick
    dim sHTML
    sHTML = window.Form1.children("DataGrid1").outerhtml
    dim oXL,oBook
    window.Form1.children("DataSheet11").copy
    'this not work for copying to clipboard
    set oXL = CreateObject("Excel.Application")
    set oBook = oXL.Workbooks.Add
    oBook.ActiveSheet.Paste
    oXL.Visible=true
    oXL.UserControl = true
    </pre>



    I tried using variable sHTML to contain the dataset but no luck.

    Any help woulfd be much appreciated.
    TIA
    Alan

  2. #2
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Copying datagrid to clipboard

    Alan

    Is it a coding solution you want or would <!post=Net Picker - Free Internet Tool ,533243>Net Picker - Free Internet Tool <!/post> be useful posted by <!profile=AlanMiller>AlanMiller<!/profile>
    Jerry

  3. #3
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Warrington, Cheshire
    Posts
    355
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Copying datagrid to clipboard

    Thanks Jerry, I'd seen the net picker but even that is too complicated for some of the persons this is aimed at.
    Simplest solution for me is to have a "Press here to export table to Excel" button.

    I got a bit further since last posting and used
    <pre> imports System.Windows.Forms
    </pre>

    from where i was able to make reference to Clipboard.setdata(myDataGrid1)
    but it all went pear shaped from there when it then replied with
    <pre> The current thread must set to Single Thread Apartment (STA) mode
    before OLE calls can be made.
    Ensure that your Main function has STAThreadAttribute marked on it.</pre>

    So i set the current thread threading to Apartment.STA and still no joy.
    This is the simplest clipboard comment I can think of but still has STA threading error
    (does this mean I am nearer to the goal):
    <pre> Clipboard.SetDataObject(txtEmployer.Text, True)</pre>


    Should be really simple and straight forward as far as I can determine, but alas, no go.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Copying datagrid to clipboard

    <P ID="edit" class=small>(Edited by jscher2000 on 14-Nov-05 11:35. )</P>The old fashioned way to copy a string to the clipboard, at least in VBA, is to use the DataObject object in the MSForms library. It only works with text, so I'm not sure whether Excel will treat sHTML as HTML or a very long string that belongs in a single cell.

    Although it's more work, you could fall back on looping through your data and inserting it into individual cells...

    Whoops, thought I was on a different board...

    This is an ASP.NET page? You should be able to generate a CSV file on the server and push it to the user with a heading like application/excel or octet-stream that forces the browser to present the save/open dialog.

Posting Permissions

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