Highlight Total Bookmark Content

Hi,

My requirement is to highlight bookmark content in a word doucment.

Please find the attached document with one book mark created.

Below is my code:

while (currentNode != bm.BookmarkEnd && currentNode != null)
{
    if (currentNode.NodeType == NodeType.Run)
    {
        ((Run)currentNode).Font.HighlightColor = Color.Yellow;
    }
    currentNode = currentNode.NextSibling;
}

Thanks

Sreedhar Dharanikota

Hi Sreedhar,

Thanks for your inquiry. Please use Node.NextPreOrder as shown below instead of Node.NextSibling property. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "BookmarksDocument.docx");
Bookmark bm = doc.Range.Bookmarks["b2"];
Node currentNode = bm.BookmarkStart;
while (currentNode != bm.BookmarkEnd && currentNode != null)
{
    if (currentNode.NodeType == NodeType.Run)
    {
        ((Run)currentNode).Font.HighlightColor = Color.Yellow;
    }
    currentNode = currentNode.NextPreOrder(doc);
}
doc.Save(MyDir + "Out.docx");