IReplacingCallback - How to ignore the matched Node inside the Bookmark

Hi,
I have a document in which some texts are wrapped in Bookmark (say “Bookmark1”) and some texts are outside bookmark.

Now I have to find certain words in the texts only outside the bookmark and replace it with something else. I am using IReplacingCallback to find and replace.

When it finds certain word it is finding the texts that are wrapped inside the bookmark also, i need to ignore finding the text that exists inside the bookmark. Could you please let me know if there is any way to check if matched Node is inside the bookmark or outside the bookmark. If it is inside the bookmark i can skip that. Thanks

Regards,
Chetan

@KCSR you can use the following code in your implementation of the IReplaceCallbackInterface to detect if the node to replace is inside a Bookmark:

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
    if(IsRunInBookMark((Run)args.MatchNode) || args.MatchNode.GetAncestor(NodeType.BookmarkStart) != null)
    {
        return ReplaceAction.Skip;
    }
    return ReplaceAction.Replace;
}

private static bool IsRunInBookMark(Run run)
{
    Node sibling = run;
    while(sibling != null && sibling.NodeType != NodeType.Body)
    {
        if (sibling.NodeType == NodeType.BookmarkStart)
        {
            return true;
        }

        sibling = sibling.PreviousSibling;
    }

    return false;
}