Reading from MS Word tables with .NET

In my project I need to get data from MS Word tables and insert it to database. I found on the net how to open word document to get tables. But when I start to read table row by row it gives me an exeption 'Cannot access individual rows in this collection because the table has vertically merged cells'. How can I read from merged rows or maybe is there a beter way to complete this task

Answer this question

Reading from MS Word tables with .NET

  • Jes_

    Right, I did It with a little difference. I was forced to read single cells first and fill it to datatable. Each cell object or in my case merged cell has index of first row and I use it for reading merged cell so I can know from which index cell starts and where it ends. So I read these indexes to array and when I reading merged cell I add Its values to relevant for single cell column in data table.

    for example:
    --------------------------------------------------
    |            |               | 1. Some property    |
    |    1      |    Data     |2 .Some property    |
    |            |                |3.Some property     |
    --------------------------------------------------

    in datatable will be: 
    -------------------------------------------------
    |    1      |   Data     | 1.Some property     |
    |    1      |   Data     | 2.Some property     |
    |    1      |   Data     | 3.Some property     |
    -------------------------------------------------
     

  • Simon

    Thanks everyone for this thread, it helped greatly.  One little problem is that once I extract the MS word table into my VB Datatable and output it, there are what looks to me like return characters after every feild value.  They output as two small squares.  I tried doing replace and trim functions with no success. 
    What is the character code I should be looking for to remove these symbols.

    Please advise.
    Thanks

    Chad


  • Borvik

    There is probably a better way, but here's a "quick and dirty" using error trapping...

    Dim i As Integer

    'First, Loop through each table; you may perfer to pick a table explicitly
    For i = 1 To Word.ActiveDocument.Tables.Count
        'Create some variables to hold row and column values
        Dim x, y As Integer

        'Loop through the rows in the table
        For x = 1 To [WordInstance].Tables(i).Rows.Count
            'Loop through the coloumns for each row
            For y = 1 To [WordInstance].Tables(i).Columns.Count
                   'Try to read the value of cell X,Y
                   Try
                       Dim CellValue as String
                       CellValue = [WordInstance].Tables(i).Cell(x, y).Range.Text)
                   Catch ex As Exception
                       'If we get an error, there is no cell at X,Y because Y is merged with the cell
                       'above, so increase Y by 1
                       y+=1
                   End Try
            Next
        Next
    Next

    This will handle vertically merged cells.  You should similarly trap the "X" loop to get horizontally merged cells.

    You'd probably be better to select a range of cells within the table and enumerate the cells of the range object, but the above code will work in a simple scenario.

  • Kevin666

    And the above logic will do this.  The merged cell will be read one time on the row that contains the first cell in the merge.  The following row will not have a cell at that column.

    For instance:

    -----------------------------------------------------
    | Merge Cell 1  | Single Cell 1 |  Single Cell 2 |
    | (white space) |----------------|-----------------|
    | (white space) | Single Cell 3 |  Single Cell 4 |
    -----------------------------------------------------

    The code supplied above would read through this table and return:

    Merge Cell 1
    Single Cell 1
    Single Cell 2
    Single Cell 3
    Single Cell 4

  • Gerald Hinson

    The problem is that I need to read from merged cell.
  • Reading from MS Word tables with .NET