Content positions need to belong to the same document!

Hi,

I’m using custom logic to remove certain ranges in my Word documents.

Sometimes the code throws a runtime error if I try to delete a ContentRange, because it don’t exist anlymore. The error message is: “Content positions need to belong to the same document!”

image

Is there a way to do a check before calling the Delete() method, so I can skip the Delete if the content is not available anymore?

Can you help me with this issue?

Thank you in advance.

Joeri

Hi Joeri,

Can you send us a small snippet code that reproduces this so we can investigate it?

Currently, I can only guess your context, so perhaps you could try using something like this:

bool invalidState = deleteRange
    .GetChildElements()
    .Any(e => e.GetParentElements(ElementType.Document).FirstOrDefault() == default);

I hope this helps.

Regards,
Mario