We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Programmatically navigate through a table (MoveToCell)

Hi,

I am having some difficulty navigating through a table which is defined in my word template. The table contains 2 rows; the first is a header row, and the second row contains 5 cells, with a bookmark in the first cell.

I want to navigate to the bookmark in the first cell of the second row, and insert some value there. I also need to go through the other 4 cells on that row, and insert appropriate data there.

Lastly, I will need to be able to add more rows beneath the second row, with the same amount of cells, and input textual values there.

When I try to use the DocumentBuilder.MoveToCell() method, I get an error message stating that the first argument, namely the table index, is out of range:

"ArgumentOutOfRangeException was unhandled. Specified argument was our of the range of valid values."

Please help me to understand what I am doing wrong.

Here is an excerpt of my code:

Aspose.Words.Document whatsUpDoc = LoadDocument(documentPath + documentName);

foreach (Aspose.Words.Bookmark bookmarkItem in whatsUpDoc.Range.Bookmarks)
{
if (bookmarkItem.Name.Equals("BookmarkImLookingFor"))
{
DocumentBuilder docBuilder = new DocumentBuilder(whatsUpDoc);
docBuilder.MoveToBookmark(bookmarkItem.Name);

Cell cellWithBookmark = docBuilder.CurrentParagraph.ParentNode as Cell;
Row rowWithBookmark = cellWithBookmark.ParentRow;
Table tableWithBookmark = rowWithBookmark.ParentTable;

// I need these indexes for the MoveToCell method
int tableIndex = tableWithBookmark.ParentNode.IndexOf(tableWithBookmark);
int rowIndex = tableWithBookmark.IndexOf(rowWithBookmark);
int cellIndex = rowWithBookmark.IndexOf(cellWithBookmark);

for (int i = cellIndex; i < rowWithBookmark.Cells.Count; i++)
{
docBuilder.MoveToCell(tableIndex, rowIndex, cellIndex, 0);
docBuilder.Write("cell " + i.ToString());
}
}
}

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think, you can try using the same technique as described here:

Also, maybe in your case it is better to use mail merge with regions. Please see the following link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/mail-merge-with-regions-explained.html

Please let me know in case of any issues, I will be glad to help you.

Best regards,

Thank you for your advice. I implemented your first suggestion, using the supplied MoveToNextCell method.

I changed the call to nextCell.RemoveAllChildren() into the following call in order to retain the formatting in the original row/cell: nextCell.FirstParagraph.RemoveAllChildren()

I also removed the nextCell.EnsureMinimum() method call, as you advised in the thread you provided the link to.