7

I am getting the below exception while reading the merged cell.

"Cannot access individual rows in this collection because the table has vertically merged cells."

My code is,

foreach (Row aRow in doc.Tables[i].Rows)
{
   foreach (Cell aCell in aRow.Cells)
   {
       MessageBox.Show(aCell.Range.Text);
   }
} 

// My table format is..

| R1C1 |R1C2|______|

| R2C1 |R2C2| R*C3 ..|

| R3C1 |R3C2|______|

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Aslam
  • 93
  • 1
  • 4

2 Answers2

8

You could try the following:

Table table = Globals.ThisDocument.Tables[1];
Range range = table.Range;
for (int i = 1; i <= range.Cells.Count; i++)
{
    if(range.Cells[i].RowIndex == table.Rows.Count)
    {
        range.Cells[i].Range.Text = range.Cells[i].RowIndex + ":" + range.Cells[i].ColumnIndex;
        MessageBox.Show(range.Cells[i].Range.Text);
    }
}
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    Thanks bro, it really helps me. any one want to use this code just remove the below line "range.Cells[i].Range.Text = range.Cells[i].RowIndex + ":" + range.Cells[i].ColumnIndex;" in above code... – Aslam Jun 25 '14 at 06:12
  • 1
    one more query regarding merged cell...:) How to find the count/number of paragraphs in the table (having merged cell)? How can I get the current paragraph number in the word document. – Aslam Jun 26 '14 at 11:01
0

Table.Range.Cells can be enumerated:

foreach (Cell cell in doc.Tables[i].Range.Cells)
{
    Debug.Print(cell.Range.Text);
}
Slai
  • 22,144
  • 5
  • 45
  • 53