Datagrid to clipboard

How can I copy a complete datagrid to the clipboard in such a way that I can paste it as a table in a Word Document?. I´m using VB6 and Word 97.


Thanks so much.

Gregorio
gsanromanAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mcriderConnect With a Mentor Commented:
Once you have the text on the clipboard, you can do this:

    Set WordDoc = CreateObject("word.basic") 'START WORD
    WordDoc.appshow 'TO HIDE THE INSTANCE OF WORD, COMMENT THIS LINE OUT
    WordDoc.filenew 'OPEN A BLANK DOCUMENT
    WordDoc.Insert Clipboard.GetText 'INSERT CLIPBOARD TEXT
    WordDoc.editselectall 'TELL WORD TO SELECT ALL
    WordDoc.TextToTable 'TELL WORD TO CONVERT TO TABLE
    WordDoc.EditCopy 'COPY THE TABLE BACK TO THE CLIPBOARD
    WordDoc.setdocumentdirty 0 'TELL WORD THE DOC CAN BE CLOSED WITHOUT ASKING
    WordDoc.filecloseall 'CLOSE ALL OF THE DOCUMENTS OPEN
    Set WordDoc = Nothing 'GET RID OF THE INSTANCE OF WORD


Now, when you paste, you are pasting a table!


Hope this helps!


Cheers!
0
 
mcriderCommented:
Here are two functions for copying and pasting cells on grids (CopyFromGrid and PasteToGrid).  These functions use the clipboard to transport the copied cells and paste them into other cells.

What you want to look at CopyFromGrid.  Currently it puts a text header on the clipboard before actually putting the data on the clipboard so that PasteToGrid can identify the clipboard data as information from CopyFromGrid... You can remove the header.

Cells are separated by vertical tabs and lines are separated by Form Feeds, but you can change that too.

Hope this helps!


Cheers!


THE CODE:


    Sub CopyFromGrid(GridObject As Object, _
        Optional StartRow As Variant, Optional EndRow As Variant, _
        Optional StartCol As Variant, Optional EndCol As Variant)
        'THIS FUNCTION WILL PASTE THE CLIPBOARD CONTENTS OF A CopyFromGrid CALL
        'IF StartRow AND StartCol ARE NOT SPECIFIED, THE CURRENT GRID ROW/COL ARE USED
        'IF EndRow AND EndCol ARE NOT SPECIFIED, THE CURRENT GRID ROWSEL/COLSEL ARE USED
        Dim Row As Integer
        Dim Col As Integer
        Dim sRow As Integer
        Dim sCol As Integer
        Dim eRow As Integer
        Dim eCol As Integer
        Dim R1 As Integer
        Dim R2 As Integer
        Dim C1 As Integer
        Dim C2 As Integer
        Dim ClipString As String
        ClipString = "[GRIDCLIP]" + vbFormFeed
        With Grid1
            sRow = .Row: If IsMissing(StartRow) = False Then sRow = StartRow
            sCol = .Col: If IsMissing(StartCol) = False Then sCol = StartCol
            eRow = .RowSel: If IsMissing(EndRow) = False Then eRow = EndRow
            eCol = .ColSel: If IsMissing(EndCol) = False Then eCol = EndCol
            If sCol > eCol Then
                C1 = eCol: C2 = sCol
            Else
                C1 = sCol: C2 = eCol
            End If
            If sRow > eRow Then
                R1 = eRow: R2 = sRow
            Else
                R1 = sRow: R2 = eRow
            End If
            For Row = R1 To R2
                For Col = C1 To C2
                    ClipString = ClipString + .TextMatrix(Row, Col) + vbVerticalTab
                Next Col
                ClipString = ClipString + vbFormFeed
            Next Row
            Clipboard.Clear
            Clipboard.SetText ClipString, vbCFText
        End With
    End Sub
    Function PasteToGrid(GridObject As Object, Optional StartRow As Variant, Optional StartCol As Variant) As Boolean
        'THIS FUNCTION WILL PASTE THE CLIPBOARD CONTENTS OF A CopyFromGrid CALL
        'IF StartRow AND StartCol ARE NOT SPECIFIED, THE CURRENT GRID ROW/COL ARE USED
        'THIS FUNCTION WILL RETURN TRUE IF SUCCESSFUL
        Dim Row As Long
        Dim Col As Long
        Dim sRow As Long
        Dim sCol As Long
        Dim ClipString As String
        Dim ClipLine As String
        Dim Cindex As Long
        On Error Resume Next
        PasteToGrid = False
        ClipString = Clipboard.GetText
        If Not (Left$(ClipString, 11) = "[GRIDCLIP]" + vbFormFeed) Then Exit Function
        ClipString = Mid$(ClipString, 12)
        With GridObject
            sRow = .Row: If IsMissing(StartRow) = False Then sRow = StartRow
            sCol = .Col: If IsMissing(StartCol) = False Then sCol = StartCol
            Row = sRow
            Do While Not (ClipString = "")
                Cindex = InStr(1, ClipString, vbFormFeed)
                ClipLine = Left$(ClipString, Cindex - 1)
                ClipString = Mid$(ClipString, Cindex + 1)
                Col = sCol
                Do While Not (ClipLine = "")
                    Cindex = InStr(1, ClipLine, vbVerticalTab)
                    .TextMatrix(Row, Col) = Left$(ClipLine, Cindex - 1)
                    If Not (Err = 0) Then Exit Function
                    ClipLine = Mid$(ClipLine, Cindex + 1)
                    Col = Col + 1
                Loop
                Row = Row + 1
            Loop
        End With
        PasteToGrid = True
    End Function
0
 
gsanromanAuthor Commented:
I Thank you McRider, I liked the code but it's not exactly what I needed. Some of the properties used in the code don´t apply to the datagrid, apply to MSFlexGrid, and when I paste the grid to a document in Word it doesn´t paste like a table, it writes every single row in a different document´s page.

I think I need to know how Excel writes a range of cells to the clipboard because when they are pasted to a Word document they appear as a table (with lines dividing each cell).
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mcriderCommented:
Sorry, It was only meant as an example.

The reason it writes each row on a different page is because, like I said, when the information is put on the clipboard, the rows are separated by Form Feeds... In case you're interrested, you would change the "vbFormFeed" to "vbCrLf" in the CopyFromGrid subroutine...

I don't believe there is another way to paste, but I could be wrong...


Cheers!
0
 
gsanromanAuthor Commented:
I think McRider's answer is very good and very close to what I want. I would like that the user of my program could copy some cells or the entire datagrid to the clipboard and then paste it to a Word document as a table (with lines dividing each cell) and no need to ask him to use the "Convert text to table" Word option.  Just as Excel does.

With McRider's answer I can paste the grid to Word and then ask the user to select the pasted data and use "Convert text to table" option.  It´s possible to avoid the user this last two steps?.
0
 
gsanromanAuthor Commented:
Thanks!.
0
 
mcriderCommented:
Thanks for the points! Glad I could help!


Cheers!
0
All Courses

From novice to tech pro — start learning today.