Updating cells in an existing table


We have some tables in a document that are 2 columns wide by x rows long. I want to use the DocumentBuilder to update the contents of column 2 of these tables, but in order to use the DocumentBuilder I need to know the table index.

I am able to update the table by placing a bookmark in the table, then finding the bookmark and its ancestor table, then updating the cells via the Table Rows/Cells collections but this seems clunky.

Alternatively, I know I can put a bookmark on EVERY cell of the tables that needs updating, but that’s clunky too.

So if I want to use the DocumentBuilder - can I programmatically find the index of a table with a Bookmark or some other method? If not, what’s the quickest way to update the data in my tables?

Thank you.


I believe, you can meet this requirement by using the following code:

Document doc = new Document("E:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bm = doc.Range.Bookmarks["bm"];
Table tab = (Table) bm.BookmarkStart.GetAncestor(NodeType.Table);
if (tab != null)
    for (int i = 0; i < tab.Rows.Count; i++)
        Row row = tab.Rows[i];
        Cell cell = row.Cells[1];
        builder.Write("new text" + i);