Find a Table using a Bookmark

Hi,
I am using a Word .dot file as a template for a pdf report. The Word .dot file will have one or more tables which I will add rows to using Aspose Words. Currently I locate the table I want to update by index, using code like this…

Table table = (Table) docMaster.getChild(NodeType.TABLE, 1, true);

This works fine as long as no other tables are added to my .dot file.
How can I use a bookmark to get the Table object instead of relying on the index number?

This message was posted using Page2Forum from BookmarkStart - Aspose.Words for Java

Hi

Thanks for your request. You can try inserting a Bookmark in a table, which you would like to find. Then you can use the following code to find the table:

Document doc = new Document("in.doc");
// Get bookmark
Bookmark bk = doc.Range.Bookmarks["bookmarkTable1"];
if (bk != null)
{
    // Get table, where bookmark is located.
    Node table = bk.BookmarkStart.GetAncestor(NodeType.Table);
    if (table != null)
    // Work with table.
}

Best regards,

Thank You Andrey,

I was trying to bookmark the table it’s self, to no avail.

I found a similar approach, using a bookmark inside one of the table cells. I called BookmarkStart.getParentNode() iteratively until getNodeType() == NodeType.TABLE.

Your technique is much cleaner and works well.

Thanks,
Mike