Find and Replace text in a Word file with C# and VB.NET

With GemBox.Document you can easily find and replace all the parts of a Word document that contain the specified text or match the specified regular expression by using one of the ContentRange.Replace methods in C# or VB.NET.

You can also search for all occurrences of a specified String or Regex using one of the ContentRange.Find methods and process the resulting ContentRange objects as needed.

This approach is useful when requiring more complex replacement, like replacing the found content with tables, pictures or some other content. For more information, see Find & Replace text with Picture or Table article.

The following example shows how you can search for all occurrences of a specified text in a Word document and highlight them or replace them with any desired text and optional formatting.

Finding and replacing or highlighting text from Word document in C# and VB.NET
Screenshot of Word document with found and replaced text
Upload your file (Drag file here)
using System.Linq;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = DocumentModel.Load("%InputFileName%");

        // 1. The easiest way how to find and replace text.
        document.Content.Replace(".NET", "C# / VB.NET");

        // 2. You can also find and highlight text by specifying the format of replacement text.
        var highlightText = "read, write, convert and print";
        document.Content.Replace(highlightText, highlightText,
            new CharacterFormat() { HighlightColor = Color.Yellow });

        // 3. You can also search for specified text and achieve more complex replacements.
        // Notice the "Reverse" method usage for avoiding any possible invalid state due to replacements inside iteration.
        var searchText = "GemBox.Document";
        foreach (ContentRange searchedContent in document.Content.Find(searchText).Reverse())
        {
            var replaceText = "Word library from GemBox called ";
            ContentRange replacedContent = searchedContent.LoadText(replaceText,
                new CharacterFormat() { FontColor = new Color(237, 125, 49) });

            var hyperlink = new Hyperlink(document, "https://www.gemboxsoftware.com/document", "GemBox.Document");
            replacedContent.End.InsertRange(hyperlink.Content);
        }

        document.Save("Find and Replace.%OutputFileType%");
    }
}
Imports System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document = DocumentModel.Load("%InputFileName%")

        ' 1. The easiest way how to find and replace text.
        document.Content.Replace(".NET", "C# / VB.NET")

        ' 2. You can also find and highlight text by specifying the format of replacement text.
        Dim highlightText = "read, write, convert and print"
        document.Content.Replace(highlightText, highlightText,
            New CharacterFormat() With {.HighlightColor = Color.Yellow})

        ' 3. You can also search for specified text and achieve more complex replacements.
        ' Notice the "Reverse" method usage for avoiding any possible invalid state due to replacements inside iteration.
        Dim searchText = "GemBox.Document"
        For Each searchedContent As ContentRange In document.Content.Find(searchText).Reverse()

            Dim replaceText = "Word library from GemBox called "
            Dim replacedContent As ContentRange = searchedContent.LoadText(replaceText,
                New CharacterFormat() With {.FontColor = New Color(237, 125, 49)})

            Dim hyperlink As New Hyperlink(document, "https://www.gemboxsoftware.com/document", "GemBox.Document")
            replacedContent.End.InsertRange(hyperlink.Content)

        Next

        document.Save("Find and Replace.%OutputFileType%")

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.