Document.Range.Replace is throwing exception when document is changed

Hi,

I want to find a text in a document and replace it with a bookmark. I am using Document.Range.Replace together with a custom evaluator. Here is an example:

public void ReplaceWithEvaluator()
{
    Document document = new Document(@"D:\Test.docx");
    document.Range.Replace(new Regex("%([A-Z0-9_]{3,30})%"), new MyReplaceEvaluator(), true);
    document.Save(@"D:\Test Out.docx");
}

private class MyReplaceEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        builder.MoveTo((Run)e.MatchNode);
        string bookmarkName = e.Match.Value;
        builder.StartBookmark(bookmarkName);
        builder.EndBookmark(bookmarkName);
        e.Replacement = "";
        return ReplaceAction.Replace;
    }
}

This always worked fine. But from version 15.11.0.0 onwards, an exception is now being thrown: “Cannot perform further replacements since the document has been changed”.

I guess this has something to do with WORDSNET-12530. But nevertheless, how can I accomplish what I need to do without an exception being thrown? Can you tell me a workaround or can you fix that?

Best regards,
Oliver

Hi Oliver,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12954. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

As a workaround of this issue, please use isForward parameter as false as shown in following code snippet.

Document document = new Document(MyDir + "Test.docx");
document.Range.Replace(new Regex("%([A-Z0-9_]{3,30})%"), new MyReplaceEvaluator(), false);

We are hitting this one as well in the latest 16.5 after upgrading to get a different fix.
The api docs don’t explain what a false value does. When would it not start from the beginning in this situation?

Hi,

Thanks for asking.

Passing false to that parameter will cause the replacement function to walk the nodes in the document backward from the end of the document to the start instead of forwards from the start of the document. Performing the search backwards can avoid issues where the document structure is changed which can cause an exception like the one shared above.

Please let me know if setting this parameter to false works for you and if you continue to have any problems please share your code and document in a new thread for testing

Thanks,

The issues you have found earlier (filed as WORDSNET-12954) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi,

I just had this exact issue in the latest release version today, has this bug returned?

Hi Aaron,

Thanks for your inquiry. Please use the latest version of Aspose.Words for .NET 17.1.0 with new Range.Replace methods.

Please check the detail of API changes from here:
Aspose.Words for .NET 16.7.0 Release Notes

If you still face problem, please share your input document along with simplified code example here for testing. We will investigate the issue and provide you more information on this.