Find and Replace text in a Word document using C# and VB.NET

Searching for words or phrases in a Word document and replacing them with other content is a common task in handling documents. For developers working on large office documents in C# and VB.NET applications, it can be as easy as doing it in Word.

This article will teach you several approaches you can take and show how to search and replace the Word documents' text using only the .NET Framework (without using any third-party code).

We have organized this article into the following sections:

Refer to this example to see the installation instructions for GemBox.Document API that we will use for this tutorial.

Find and replace text in a Word document with Microsoft options

Find and replace method in C#

There are a few options for performing find and replace text in Word documents programmatically. If you want to use Word Automation (which requires having MS Word installed), you can do the find and replace action with an API provided by Word Interop, as demonstrated here.

How to use GemBox.Document to find and replace text in Word

GemBox.Document is a .NET component for processing Word files that presents a document with a content model hierarchy that can be accessed as flat content through the ContentRange class. With it, we can search for content that spans multiple paragraphs.

With this approach, you can easily find all the parts of a Word document that contain the specified text or match the specified regular expression (including tables, pictures, paragraphs, HTML formatted text, RTF formatted text, etc.) by using one of the ContentRange.Replace methods.

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 you need a more complex replacement, like replacing your placeholders with hyperlinks, tables, pictures, or other content.

In the following sections, you will learn how to find and replace text in several circumstances.

Find and replace text in a Word document using String

For simple search and replace, you can just set the string you want to find in your document. For that, you will use the Find(string) method. To work with this method, follow the next steps:

  1. Make sure you call the ComponentInfo.SetLicense method before using any other member of the library. Since we are working with a console application, we propose you put this line at the beginning of the Main() method.
    ComponentInfo.SetLicense("FREE-LIMITED-KEY");
    ComponentInfo.SetLicense("FREE-LIMITED-KEY")
  2. In this case, the code will create a simple example document to show the available data. But you can also load any document using any of theDocumentModel.Load() methods.
    var document = new DocumentModel();
    document.Sections.Add(new Section(document,
       new Paragraph(document, "Name: [NAME]."),
       new Paragraph(document, "Age 18"),
       new Paragraph(document, "Email: [EMAIL]")));
    Dim document = New DocumentModel()
    document.Sections.Add(New Section(document,
        New Paragraph(document, "Name: [NAME]."),
        New Paragraph(document, "Age 18"),
        New Paragraph(document, "Email: [EMAIL]")))
  3. Next, you will find the placeholders and change them using various methods. In this first code section, you will replace the '[NAME]' placeholder with 'John Doe' using the LoadText method.
    var nameRange = document.Content.Find("[NAME]").First();
    nameRange.LoadText("John Doe");
    Dim nameRange = document.Content.Find("[NAME]").First()
    nameRange.LoadText("John Doe")
  4. Then you will directly replace the '[EMAIL]' placeholder with 'john@doe.com'.
    document.Content.Replace("[EMAIL]", "john@doe.com");
    document.Content.Replace("[EMAIL]", "john@doe.com")
  5. Last, you will append the text before and after a specific text by using ContentRange.Start and ContentRange.End properties, and save the document.

    var ageRange = document.Content.Find(" 18").First();
    ageRange.Start.LoadText(":");
    ageRange.End.LoadText(" years old");
    document.Save("Output.docx");
    Dim ageRange = document.Content.Find(" 18").First()
    ageRange.Start.LoadText(":")
    ageRange.End.LoadText(" years old")
    document.Save("Output.docx")

Find and replace text in a Word document using Regex

You can use regular expressions to check for repeated occurrences of words in a string using the ContentRange.Find(Regex) method. Follow the next steps to learn how to use it.

  1. Here the code will create a new simple document with data, so you can better visualize the document's content that you will later search with Regex. You can also load any document using the DocumentModel.Load() method.
    var document = new DocumentModel();
    document.Sections.Add(new Section(document,
        new Paragraph(document, "Name: [NAME]"),
        new Paragraph(document, "Age:  [AGE]"),
        new Paragraph(document, "Email: [EMAIL]")));
    var document = new DocumentModel();
    document.Sections.Add(new Section(document,
        new Paragraph(document, "Name: [NAME]"),
        new Paragraph(document, "Age:  [AGE]"),
        new Paragraph(document, "Email: [EMAIL]")));
  2. Get all ranges that match the specified Regex pattern. In this case we are looking for all words enclosed with '[ ]'.
    var pattern = "\\[.*?\\]";
    var ranges = document.Content.Find(new Regex(pattern));
    Dim pattern = "\[.*?\]"
    Dim ranges = document.Content.Find(New Regex(pattern))
  3. Next, you will replace the placeholders according to the keyword you found. Note that the replacements need to be done in reverse to avoid a possible invalid state because you are changing the document while iterating through it.
    foreach (var range in ranges)
        switch (range.ToString())
        {
            case "[NAME]": range.LoadText("John Doe"); break;
            case "[AGE]": range.LoadText("18 years old"); break;
        }
    For Each range In ranges
        Select Case range.ToString()
            Case "[NAME]" : range.LoadText("John Doe")
            Case "[AGE]" : range.LoadText("18 years old")
        End Select
    Next
  4. Note that you can also use regex to find and replace content directly if you already have specific data in mind.
    document.Content.Replace(new Regex("\\[EMAIL\\]"), "john@doe.com");
    document.Content.Replace(New Regex("\[EMAIL\]"), "john@doe.com")

Find text in a document and highlight it

When using GemBox.Document you can also find parts of the text in a Word document and highlight them programmatically.

The following code examples illustrate how to highlight all occurrences of a specific word.

  1. After setting up your serial key, you will load the desired document. Again, in this case, we will create a new simple document so that you can easily verify the result.
    var document = new DocumentModel();
    document.Sections.Add(new Section(document,
        new Paragraph(document, "First Paragraph"),
        new Paragraph(document, "Second Paragraph"),
        new Paragraph(document, "Third Paragraph")));
    Dim document = New DocumentModel()
    document.Sections.Add(New Section(document,
        New Paragraph(document, "First Paragraph"),
        New Paragraph(document, "Second Paragraph"),
        New Paragraph(document, "Third Paragraph")))
  2. Next, you will find each occurrence of the word or sentence you need to highlight. In this case, we will search for 'Paragraph'.
    var ranges = document.Content.Find("Paragraph");
    foreach (var range in ranges)
    {
        // The code from steps 3 to 5 goes here...
    }
    Dim ranges = document.Content.Find("Paragraph")
    For Each range In ranges
        ' The code from steps 3 to 5 goes here...
    Next
  3. For every found occurrence you will first duplicate it and clone its CharacterFormat.
    var highlighted = new Run(document, range.ToString());
    highlighted.CharacterFormat = ((Run)range.Start.Parent).CharacterFormat.Clone();
    Dim highlighted = New Run(document, range.ToString())
    highlighted.CharacterFormat = CType(range.Start.Parent, Run).CharacterFormat.Clone()
  4. You will then highlight it with any desired color. In this example you will use the red color.
    highlighted.CharacterFormat.HighlightColor = Color.Red;
    highlighted.CharacterFormat.HighlightColor = Color.Red
  5. At the end, replace the original unformatted word "Paragraph" with the new highlighted content.
    range.Set(highlighted.Content);
    range.Set(highlighted.Content)
  6. Once you are done with highlighting all the occurrences, save the changes to a new document.
    document.Save("Output.docx");
    document.Save("Output.docx")

Find text in a Word document and format it using C#

After writing a lengthy document, you may need to format specific parts of the text. And to optimize this process, you can use the find and replace feature.

You can follow the next steps to find and format text:

  1. Load the document that you need to format.
    var document = DocumentModel.Load("[document path]");
    Dim document = DocumentModel.Load("[document path]")
  2. Execute the code below to find the desired text, setting the Find() method parameter to a value that corresponds to what you need to find.
    var range = document.Content.Find("Example text").First();
    Dim Range = document.Content.Find("Example text").First()
  3. Replace the found instance with the exact same text but with a different format, and save the changes.
    range.LoadText(range.ToString(), new CharacterFormat() 
        { FontColor = Color.Blue, Size = 20, Italic = true });
    
    document.Save("Output.docx");
    Range.LoadText(Range.ToString(), New CharacterFormat() 
        With { .FontColor = Color.Blue, .Size = 20, .Italic = True })
    
    document.Save("Output.docx")

Find text in a DOCX document and delete it

When you find an error in a document, which was replicated several times, you may need to delete all instances. Doing that automatically can save you a lot of time.

With GemBox.Document, you can simply find the errors and delete them with the Delete().

  1. For the purpose of demonstrating the removal od invalid text parts, we will create a simple document.
    var document = new DocumentModel();
    document.Sections.Add(new Section(document,
        new Paragraph(document, "First Paragraph"),
        new Paragraph(document, "Second Paragraph"),
        new Paragraph(document, "Third Paragraph")));
    Dim document = New DocumentModel()
    document.Sections.Add(New Section(document,
        New Paragraph(document, "First Paragraph"),
        New Paragraph(document, "Second Paragraph"),
        New Paragraph(document, "Third Paragraph")))
  2. Next, find all occurrences of 'Paragraph' and delete them.
    var ranges = document.Content.Find("Paragraph");
    foreach (var range in ranges)
        range.Delete();
    
    document.Save("Output.docx");
    Dim ranges = document.Content.Find("Paragraph")
    For Each range In ranges
        range.Delete()
    Next
    
    document.Save("Output.docx")

Conclusion

In this article, you learned several methods of finding and replacing text in Word documents using C# and VB.NET. Using this feature, you also learned how to manipulate and format parts of the text you searched for.

For more information regarding the GemBox.Document API, check the product documentation pages. We also recommend checking our GemBox.Document examples where you can examine other features and even run the example codes to test them.

See also


Next steps

GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API. How about testing it today?

Download Buy