As part of Implementing Aspose.Words.IReplacingCallback.Replacing I remove all the runs:
' Now remove all runs in the sequence.
For Each currentRun As Run In runs
currentRun.Remove()
Next
However this seems to error every so oftern with “Cannot remove because there is no parent”. I can resolve this with a null check however what is this correct and what side effect will it have?
' Now remove all runs in the sequence.
For Each currentRun As Run In runs
If currentRun.ParentNode IsNot Nothing Then
currentRun.Remove()
End If
Next
Thanks
Hi
Thanks for your inquiry. Such exception can occur because node is not inserted into the document. So your workaround is correct, you do not need to remove node if it is not preset in document at all.
Please let me know if you need more assistance, I will be glad to help you.
Best regards,
I do not fully understand why the node is not in the document as I opened the document before doing the replace?
Thanks
Hi John,
Thanks for your inquiry. This might occur if you removed parent of this node earlier. For example let’s consider the following code:
// Open document
Document doc = new Document(@"C:\Temp\in.doc");
// Get first run in the paragraph.
Run run = doc.FirstSection.Body.FirstParagraph.Runs[0];
doc.FirstSection.Remove();
// Now remove parent of run from the document.
run.ParentNode.Remove();
// Now if you atempt to remove run you will get an exception because you already removed parent of this node earlier.
run.Remove();
Hope this helps.
Best regards,