Re: Trying to delete everything between two bookmarks

Hi

I have upgraded Aspose.Words to version 15.1 and your code breaks at the line

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

with error: Object reference not set to an instance of an object

Can you please help?

Best regards

Hi Ole,

Thanks for your inquiry. Could you please attach your input Word document and piece of code here for testing? We will investigate the issue on our end and provide you more information.

Best regards,

Hi

This is a followup on an exsisting thread:
https://forum.aspose.com/t/102119

I don’ know how this turned into a new thread.

You can use the code and example from the thread above.

Best regards.

Ole Kirkholt

Hi Ole,

Thanks for your inquiry and sorry for . Please use the following code to fix this issue:

Document doc = new Document(MyDir + @"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" + "_0"].BookmarkStart;
                    doc.Range.Bookmarks["BS__KUNDE_MED_CPR" + "_0"].Name = "BS__KUNDE_MED_CPR";
                }
            }
            nextNode = curNode.NextSibling;
            curNode.Remove();
        }
    }
    else
    {
        curNode.Remove();
    }
    curNode = nextNode;
}
doc.Save(MyDir + @"15.2.0.docx");

I hope, this helps.

Best regards,

Hello

It works. Thanks for your help.

Best regards

A post was split to a new topic: Deleting Everything between Node A and Node B in Word Document | C# NET