Sign In  Sign Up Live-Chat

Trying to delete everything between two bookmarks

Last post 10-11-2008, 3:58 PM by alexey.noskov. 1 replies.
Sort Posts: Previous Next
  •  10-10-2008, 5:56 PM 147547

    Trying to delete everything between two bookmarks

    Hello,

    I have a Word template that has a starting Bookmark, then a bunch of text, paragraphs, etc., and then an ending Bookmark.

    I want to delete everything between the two bookmarks. What is the best way of doing this? Please provide me with an example that starts with the first bookmark, then examines all the nodes in between the two bookmarks (removing them if necessary) and stops at the last bookmark.

    Thanks!

     
  •  10-11-2008, 3:58 PM 147588 in reply to 147547

    Re: Trying to delete everything between two bookmarks

    Hi

     

    Thanks for your inquiry. You can try using the following code to achieve this.

     

    Document doc = new Document(@"Test138\in.doc");

     

    //Get start node

    Node startNode = doc.Range.Bookmarks["start"].BookmarkEnd;

    //Get End node

    Node endNode = doc.Range.Bookmarks["end"].BookmarkStart;

     

    Node curNode = startNode.NextPreOrder(doc);

    while (curNode!=null)

    {

        if (curNode.Equals(endNode))

            break;

     

        //move to next node

        Node nextNode = curNode.NextPreOrder(doc);

        //Check whether current contains end node

        if (curNode.IsComposite)

        {

            if (!(curNode as CompositeNode).ChildNodes.Contains(endNode) &&

                !(curNode as CompositeNode).ChildNodes.Contains(startNode))

            {

                nextNode = curNode.NextSibling;

                curNode.Remove();

            }

        }

        else

        {

            curNode.Remove();

        }

        curNode = nextNode;

    }

     

    //Save output document

    doc.Save(@"Test138\out.doc");

     

    Please let me know in case of any issues. Also please attach your document for testing.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML