How to delete content/text after bookmark in Word

hi
How to delete content/text after bookmark in Word,
just leave bookmark and content/text before it
thanks!
ken

Hi Ken,

Thanks for your inquiry. You can try using code like the following to remove content before and after bookmark:

///
/// Removes all content before specified node
///
public void RemoveContentBeforeNode(Document doc, Node endNode)
{
    Node curNode = endNode.PreviousPreOrder(doc);
    while (curNode != null)
    {
        // Move to next node
        Node nextNode = curNode.PreviousPreOrder(doc);
        // Check whether current contains start node
        if (curNode.IsComposite)
        {
            if (!(curNode as CompositeNode).GetChildNodes(NodeType.Any, true).Contains(endNode))
            {
                nextNode = curNode.PreviousSibling;
                curNode.Remove();
            }
        }
        else
        {
            curNode.Remove();
        }
        curNode = nextNode;
    }
}
///
/// Removes all content after specified node
///
public void RemoveContentAfterNode(Document doc, Node startNode)
{
    Node curNode = startNode.NextPreOrder(doc);
    while (curNode != null)
    {
        // Move to next node
        Node nextNode = curNode.NextPreOrder(doc);
        // Check whether current contains start node
        if (curNode.IsComposite)
        {
            if (!(curNode as CompositeNode).GetChildNodes(NodeType.Any, true).Contains(startNode))
            {
                nextNode = curNode.NextSibling;
                curNode.Remove();
            }
        }
        else
        {
            curNode.Remove();
        }
        curNode = nextNode;
    }
}

In your case, startNode is BookmarkEnd of your bookmark, if you need to remove content after bookmark. If you need to remove content before bookmark endNode is BookmarkStart of your bookmark.
Best regards,

hi alexey
How can I get the bookmarkEnd node which is named “namebookmark”,That means how to pass value to parameter “startNode”
thanks!

Hi

Thanks for your inqiry. You can use code like the following:

BookmarkStart start = doc.Range.Bookmarks["mybookmark"].BookmarkStart;
BookmarkEnd end = doc.Range.Bookmarks["mybookmark"].BookmarkEnd;

Best regards,

hi

Document doc = new Document("C:\\cwl.doc");
RemoveContentAfterNode(doc, doc.Range.Bookmarks["mybookmark"].BookmarkEnd);

I like this, and can not delete content / text after bookmark

thanks

Hi

Thanks for your request. Please attach your input document here for testing. I will check it and provide you more information.
Best regards,

hi
Can you give me the complete code?
thanks!

hi
I give you word document, you check the trouble
thanks

Hi

Thank you for additional information. Please try using the following modified version of the method:

///
/// Removes all content after specified node
///
public void RemoveContentAfterNode(Document doc, Node startNode)
{
    Node curNode = startNode.NextPreOrder(doc);
    while (curNode != null)
    {
        // Move to next node
        Node nextNode = curNode.NextPreOrder(doc);
        // Check whether current contains start node
        if (curNode.IsComposite)
        {
            if (!(curNode as CompositeNode).GetChildNodes(NodeType.Any, true).Contains(startNode))
            {
                if (curNode.NextSibling != null)
                    nextNode = curNode.NextSibling;
                curNode.Remove();
            }
        }
        else
        {
            curNode.Remove();
        }
        curNode = nextNode;
    }
}

Hope this helps.
Best regards,