Problem with Range.Replace-function

Hello Aspose-Team,

We try to update our Aspose.Words to the newest version.

Old Aspose.Words: Version: 17.5.0.0
New Aspose.Words: Version: 22.5.0.0

After update, we have a problem with the Range.Replace-function.

How can we fix that?

Code-Example:

GetDocument.Range.Replace(GetTokenRegex(null), WRC_InitializeTargetDict.GetNewInitializeDictReplacer(this), true);

   private Regex GetTokenRegex(string token)
    {
        StringBuilder strb = new StringBuilder();

        strb.Append("({)");
        if (token == null || token.Length == 0)
        {
            strb.Append(@"([\w\d]*)");
        }
        else
        {
            strb.Append("(" + token + ")");
        }

        strb.Append("(})");
        return new Regex(strb.ToString());
    }

internal class WRC_InitializeTargetDict : WordReplacerClassBase, IReplacingCallback
{
    private WRC_InitializeTargetDict()
    {
    }

    public static WRC_InitializeTargetDict GetNewInitializeDictReplacer(WordWorker p_wordWorker)
    {
        WRC_InitializeTargetDict ret = new WRC_InitializeTargetDict();

        ret.LocalWordWorker = p_wordWorker;

        return ret;
    }

    public ReplaceAction Replacing(ReplacingArgs args)
    {
        return WRPL_InitializeTargetDict(args);
    }

    public ReplaceAction WRPL_InitializeTargetDict(ReplacingArgs replaceArgs)
    {
        string token = replaceArgs.Match.Groups[2].Value;
        MappingTargetDOC target_doc = MappingTargetDOC.GetNewFromToken(token, LocalWordWorker.Targets_Raw.Count);

        if (!LocalWordWorker.Targets_Raw.ContainsKey(token))
        {
            LocalWordWorker.Targets_Raw.Add(token, target_doc);
            LocalWordWorker.TokenNameList_Raw.Add(token);
        }

        return ReplaceAction.Skip;
    }
}

Thank’s for your help,
Matthias

@Matthias_Winzer In newer version of Aspose.Words you should specify replacing callback in FindReplaceOptions. You should modify your code like this:

FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = WRC_InitializeTargetDict.GetNewInitializeDictReplacer(this);
GetDocument.Range.Replace((GetTokenRegex(null), "", options);

Hi Alexey,

that was helpful and is working.

Thanks,
Matthias

1 Like