How to cycle through all cells in a word table which is having split cells

I run into this situation when I am trying to extract data from (sometimes malformed) tables. This is how I handle it:

Check the number of columns

For each row in table.rows
  if row.cells.count < expectedRows
    'You know you are lacking rows
  else
    'Normal processing
  end if
Next

or Go through each cell if you want all the data anyway

For each row in table.rows
  For each cell in row.cells
    'Process individual cells
  Next
Next

None of those work if cells were merged vertically.


You should assume that each of the row has the maximum possible number of columns. In your situation, that would be four. To iterate through each cell, I propose to set On Error Resume Next before the first loop starts. Then inside your inner loop, try this code:

strCellText = itable.cell(Row, Col).Range.Text

If Err.Number = 0 Then
    uResp = uResp & Trim(strCellText)
    Debug.Print Row, Col, strCellText
Else
    Err.Clear
End If

If you want to go through all cells in all tables in a MS Word Document, even if the cells are merged, I got the results I wanted. Try this:

Sub CheckingInTheCell
Dim C as Cell
Dim tableCount, Ctr

tableCount = ActiveDocuments.tables.count

for Ctr = 1 to tableCount
   For each C in ActiveDocument.Tables(Ctr).Range.cells
       .....your validations or whatever you would like to do for the cell content
   next C
next Ctr

End Sub