We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Find Keyword in Word document & Replace with HTML String using C# .NET

Is there an updated version of this code for v21.5.0? I have tried to run this code but I get this error

System.NullReferenceException: ‘Object reference not set to an instance of an object.’

from this line in the SplitRun method

afterRun.Text = run.Text.Substring(position);


The following C# code of Aspose.Words for .NET API should replace a keyword/token in MS Word document with HTML string:

string htmlToReplaceWith = "<b><font color='red'>James Bond, </font></b>";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello <CustomerName>,");

FindReplaceOptions options = new FindReplaceOptions(FindReplaceDirection.Backward);
options.ReplacingCallback = new ReplaceWithHtmlEvaluator(htmlToReplaceWith);

doc.Range.Replace(new Regex(@"<CustomerName>,"), String.Empty, options);


private class ReplaceWithHtmlEvaluator : IReplacingCallback
    private string _html = "";

    internal ReplaceWithHtmlEvaluator(string html)
        _html = html;

    //This simplistic method will only work well when the match starts at the beginning of a run. 
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        // The first (and may be the only) run can contain text before the match,
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        // This array is used to store all nodes of the match for further removing.
        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node.
            // Have to loop because there could be other nodes such as BookmarkStart etc.
                currentNode = currentNode.NextSibling;
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
            SplitRun((Run)currentNode, remainingLength);

        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

        // Replace '<CustomerName>' text with a red bold name.

        foreach (Run run in runs)

        return ReplaceAction.Skip;

    private static Run SplitRun(Run run, int position)
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring((0), (0) + (position));
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;

In case the problem still remains, then please ZIP and upload your input Word document you are getting this problem with and source code here for testing. We will then investigate the issue on our end and provide you more information.