Create bookmark around a table

Given a cell node within a table, I would like to create a bookmark around the table.

Something like

Dim DocWriter As New Aspose.Words.DocumentBuilder(oWordDoc)
Dim ContainingTableBoundaryParagraph As Aspose.Words.Node = Nothing
Dim ContainingTableStartBoundaryParagraph As Aspose.Words.Node = Nothing
ContainingTableBoundaryParagraph = oCell.FirstParagraph
ContainingTableStartBoundaryParagraph = ContainingTableBoundaryParagraph

If Not ContainingTableBoundaryParagraph Is Nothing Then
    DocWriter.MoveTo(ContainingTableBoundaryParagraph)
    DocWriter.StartBookmark(ContainingTableMark.Name)
    ContainingTableBoundaryParagraph = oCell.ParentRow.ParentTable.LastRow.LastCell.LastParagraph
    ContainingTableBoundaryParagraph = GetNextNodeOfType(ContainingTableBoundaryParagraph, Aspose.Words.NodeType.Paragraph)

    If ContainingTableBoundaryParagraph Is ContainingTableBoundaryParagraph Then ContainingTableBoundaryParagraph = GetNextNodeOfType(ContainingTableBoundaryParagraph, Aspose.Words.NodeType.Paragraph)

    If Not ContainingTableBoundaryParagraph Is Nothing Then DocWriter.MoveTo(ContainingTableBoundaryParagraph)
    DocWriter.EndBookmark(ContainingTableMark.Name)
    DocWriter.MoveTo(oCell.LastParagraph)

Else
    TraceMsg(String.Format("WARNNIG: Could not redefine [{0}], prior paragraph not found", ContainingTableMark.Name))
End If

The start of the table seems to be correct, but not the end - how do I move to the correct node at the end of the table.

For illustration, the attached word doc has several bookmarks of this style all called TABLExyz

TABLECI is ok – when the bookmark is selected it highlights the entire table
TABLECC is created by the code above and does not span the table.

Observer what happens if you use INSERT | BOOKMARK dialog and GOTO each.

Hi Stan,

Thanks for your inquiry.

First of all, please note that, by Aspose.Words design, bookmark’s nodes (BookmarkStart and BookmarksEnd) are inline level nodes. This mean that BookmarkStart and BookmarkEnd can be children of paragraphs only. However, bookmark start and bookmark end can be placed in different paragraphs. This is described in the documentation here:
https://reference.aspose.com/words/net/aspose.words/bookmarkstart/

Moreover, in order to encapsulate a whole table inside a Bookmark, you need to place Bookmark Start node in the previous Paragraph of table and Bookmark End node inside the Paragraph i.e. following the table.

We will consider improving table bookmarks in one of future versions. Your request has been linked to the appropriate feature. You will be informed via this forum thread once this feature is available.

If we can help you with anything else, please feel free to ask.

Best Regards,

I fully understand that BookmarkStart and BookmarkEnd are inline “inside” a paragraph node, and they can be inside two different paragraphs.
The question boils down to how do I find the paragraph after table correctly.
I tried this code (extracted from the original code above)

ContainingTableBoundaryParagraph = oCell.ParentRow.ParentTable.LastRow.LastCell.LastParagraph
ContainingTableBoundaryParagraph = GetNextNodeOfType(ContainingTableBoundaryParagraph, Aspose.Words.NodeType.Paragraph)

The code attempts to go to the last paragraph in the last cell and then move to the very next paragrph node and use that as the bookmarkend.
This does not seem to work.
Is that what you are saying requires enhancement in a future release…that it should work and does not?

Hi Stan,

Thanks for your inquiry. In your case, to be able to encapsulate your table within a bookmark, please try using the following code snippet:

Document doc = new Document(@"c:\temp\GC-123000+Agenda+0005.docx");

Bookmark bookmark = doc.Range.Bookmarks["TableCC"];
BookmarkStart bookmarkStart = bookmark.BookmarkStart;
BookmarkEnd bookmarkEnd = bookmark.BookmarkEnd;

Node table = bookmarkStart.GetAncestor(NodeType.Table);

Paragraph previous = (Paragraph)table.PreviousSibling;
Paragraph next = (Paragraph) table.NextSibling;

previous.AppendChild(bookmarkStart);
next.AppendChild(bookmarkEnd);

doc.Save(@"c:\temp\out.docx");

I hope, this will help.

Best Regards,

The issues you have found earlier (filed as WORDSNET-721) have been fixed in this Aspose.Words for .NET 18.9 update and this Aspose.Words for Java 18.9 update.

@StanYork

The support of block, cell, and row levels bookmark is added in Aspose.Words 18.9. The position of BookmarkStart and BookmarkEnd nodes is according to document structure on import and export of Word documents using Aspose.Words 18.9 and later versions.