The Danger in Accessing Row Objects of Word Tables
Recently, our QA team has shown me an error they received while using my Word addin... The error said "Cannot access individual rows in this collection because the table has vertically merged cells". What what what!?!?!?
After investigating the problem and looking over the Internet, I've found that Word won't let you, the miserable programmer, to use any row related property when the table has vertically merged cells... I bet that when MS developers look at this post, they point the finger at it and laugh "Muhahahaha"...
In order to prevent this error, don't use the Row object! use Cell instead to do whatever you need to do.
If you need a cell in a certain row, do this by using the Cell.RowIndex property (you can't touch the Cell.Row property, remember...).
A helpful code to get the last cell of the table:
private Cell GetLastCell(Table tbl)
{
Cell _cell = null;
Cell _nextCell = null;
_cell = _nextCell = tbl.Cell(1, 1);
while (_nextCell != null)
{
try
{
_cell = _nextCell;
_nextCell = _cell.Next;
}
catch
{
_nextCell = null;
}
}
return _cell;
}
|
All the best,
Shay.