none
copy data from word table to excel using vb.net RRS feed

  • Question

  • Hello,

    Some might recognize this thread as I asked a previous question going the opposite way (excel to word). Now however, I would like to take data from a table in Word (2007) and copy to a spreadsheet in Excel (2007).

    Any help would be greatly appreciated. I did take a look around the net and this site and have not had luck. It seems like I might be the only (or one of the few) to go in this direction lol.
    -DP


    Join the darskide. We have cookies!



    Monday, June 4, 2012 8:25 PM

Answers

  • Hello,

    So it looks like I might have found the solution to my problem. I'm not sure if it's the 'proper way' but it gets the job done. If anyone wants to comment feel free to post - I'm open to other opinions/suggestions:

    Dim newXL As Excel.Application = Nothing
            Dim newWBS As Excel.Workbooks = Nothing
            Dim newWB As Excel.Workbook = Nothing
            Dim newWS As Excel.Worksheet = Nothing
            Dim newWord As Word.Application = Nothing
            Dim newDocs As Word.Documents = Nothing
            Dim newDoc As Word.Document = Nothing
            newXL = New Excel.Application()
            newXL.Visible = True
            newWBS = newXL.Workbooks
            newWB = newWBS.Open(RFMFile)
            newWS = newWB.Worksheets(1)
            newWord = New Word.Application()
            newDocs = newWord.Documents
            newDoc = newDocs.Open(wordFile)
            Dim objTable = newDoc.Tables(1)
            For Each t In newDoc.Tables
                For rw = 1 To t.rows.count
                    For cel = 1 To t.rows(rw).cells.count
                        newWS.Cells(rw, cel) = objTable.Cell(rw, cel).Range.Text
                    Next
                Next
            Next
            newDoc.Close()
            newWord.Quit()
        End Sub

    Please not that this is not the entire code, just the relevant part to this question. Here's the link that I referenced for this code:
    http://www.vbforums.com/showthread.php?t=659828#post4064155 (POST 4)

    Thanks again to all.
    -DP


    Join the darskide. We have cookies!


    Wednesday, June 6, 2012 3:11 PM

All replies

  • Hi,

    Based on your issue, I would direct you to these following links which can assist you in copying a word table to excel by steps.

    http://office.microsoft.com/en-us/excel-help/copy-a-word-table-into-excel-HP010254130.aspx

    andhttp://techblissonline.com/copy-word-tables-to-excel/

    Hope it can help.

    Thanks,

    Leo.

    Tuesday, June 5, 2012 1:57 AM
  • Dim newXL As Excel.Application = Nothing Dim newWBS As Excel.Workbooks = Nothing Dim newWB As Excel.Workbook = Nothing Dim newWS As Excel.Worksheet = Nothing Dim newWord As Word.Application = Nothing Dim newDocs As Word.Documents = Nothing Dim newDoc As Word.Document = Nothing newXL = New Excel.Application() newXL.Visible = True newWBS = newXL.Workbooks newWB = newWBS.Open(RFMFile) newWS = newWB.Worksheets(1) newWord = New Word.Application() newDocs = newWord.Documents newDoc = newDocs.Open(wordFile) Dim objTable = newDoc.Tables(1) newWS.Cells("A1") = objTable.Cell(1, 1).Range.Text newWS.Cells("A1") = newDoc.Tables(1).Cell(1, 1).Range.Text newDoc.Close()

    Hi Leo,

    Thanks for the response. I should have been more specific with my question: I need to do this with vb.net. I know how to copy as suggested above, but I need to do this programmatically. I have changed the question to reflect this and also added code to assist anyone who can help. One would think this would be simple, but I'm probably making this harder than it needs to be.

    I'm wondering if this could be done using a datagridview? Maybe the table could be exported/copied to this and then the data transfered from that into Excel.

    Thanks to all
    -DP



    • Edited by Darth Probius Wednesday, June 6, 2012 1:24 PM added code
    Tuesday, June 5, 2012 11:15 AM
  • Hello,

    So it looks like I might have found the solution to my problem. I'm not sure if it's the 'proper way' but it gets the job done. If anyone wants to comment feel free to post - I'm open to other opinions/suggestions:

    Dim newXL As Excel.Application = Nothing
            Dim newWBS As Excel.Workbooks = Nothing
            Dim newWB As Excel.Workbook = Nothing
            Dim newWS As Excel.Worksheet = Nothing
            Dim newWord As Word.Application = Nothing
            Dim newDocs As Word.Documents = Nothing
            Dim newDoc As Word.Document = Nothing
            newXL = New Excel.Application()
            newXL.Visible = True
            newWBS = newXL.Workbooks
            newWB = newWBS.Open(RFMFile)
            newWS = newWB.Worksheets(1)
            newWord = New Word.Application()
            newDocs = newWord.Documents
            newDoc = newDocs.Open(wordFile)
            Dim objTable = newDoc.Tables(1)
            For Each t In newDoc.Tables
                For rw = 1 To t.rows.count
                    For cel = 1 To t.rows(rw).cells.count
                        newWS.Cells(rw, cel) = objTable.Cell(rw, cel).Range.Text
                    Next
                Next
            Next
            newDoc.Close()
            newWord.Quit()
        End Sub

    Please not that this is not the entire code, just the relevant part to this question. Here's the link that I referenced for this code:
    http://www.vbforums.com/showthread.php?t=659828#post4064155 (POST 4)

    Thanks again to all.
    -DP


    Join the darskide. We have cookies!


    Wednesday, June 6, 2012 3:11 PM