We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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!

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

Hello

I have tried the code above, and it works great until you put a section-break in between the two bookmarks in the document.

Can you please change the code to handle sections-breaks as well

I have attached a test document where I want to delete everything between BB__KUNDE_MED_CPR and BS__KUNDE_MED_CPR

Best regards

Ole Kirkholt

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Please try using the following code:

Document doc = new Document(@"C:\Temp\Gruppeliv-ny-4.dot");

//Get start node

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

//Get End node

Node endNode = doc.Range.Bookmarks["BS__KUNDE_MED_CPR"].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))

{

//If there current node is end of section

//We should append content of the next section to curretn Sestion

if (curNode.NextSibling == null)

{

//Get current Section

Section curSect = (Section)curNode.GetAncestor(NodeType.Section);

if (curSect.NextSibling != null)

{

curSect.AppendContent((Section)curSect.NextSibling);

//remove next section

curSect.NextSibling.Remove();

//Reassign endNode

endNode = doc.Range.Bookmarks["BS__KUNDE_MED_CPR"].BookmarkStart;

}

}

nextNode = curNode.NextSibling;

curNode.Remove();

}

}

else

{

curNode.Remove();

}

curNode = nextNode;

}

//Save output document

doc.Save(@"C:\Temp\out.doc");

Hope this helps.

Best regards.