How to find and replace any text in an existing DOCX document
using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Find and Replace specific text.
  4. Save the document back.

Here we'll show you how to using the find and replace methods.
Using regular expressions, we'll find - "Bean" (bean, BEAN, bEan, etc) and replace to - "Joker".


Complete code

using System;
using System.IO;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            FindAndReplace();
        }
        /// <summary>
        /// Find and replace a specific text in an existing DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-and-replace-text-in-docx-document-net-csharp-vb.php
        /// </remarks>
        public static void FindAndReplace()
        {
            // Path to the loadable document.
            string loadPath = @"..\..\..\example.docx";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Find "Bean" and Replace everywhere on "Joker"
            Regex regex = new Regex(@"Bean", RegexOptions.IgnoreCase);

            // Start:

            // Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            }

            // End:

            // The code above finds and replaces the content in the whole document.
            // Let us say, you want to replace a text inside shape blocks only:

            // 1. Comment the code above from the line "Start" to the "End".
            // 2. Uncomment this code:
            //foreach (Shape shp in dc.GetChildElements(true, ElementType.Shape).Reverse())
            //{
            //    foreach (ContentRange item in shp.Content.Find(regex).Reverse())
            //    {
            //        item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            //    }
            //}

            // Save the document as DOCX format.
            string savePath = Path.ChangeExtension(loadPath, ".replaced.docx");
            dc.Save(savePath, SaveOptions.DocxDefault);

            // Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions

Namespace Sample
    Friend Class Sample
        Shared Sub Main(ByVal args() As String)
            FindAndReplace()
        End Sub
        ''' Get your free 100-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Find and replace a specific text in an existing DOCX document.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/find-and-replace-text-in-docx-document-net-csharp-vb.php
        ''' </remarks>
        Public Shared Sub FindAndReplace()
            ' Path to the loadable document.
            Dim loadPath As String = "..\..\..\example.docx"

            Dim dc As DocumentCore = DocumentCore.Load(loadPath)

            ' Find "Bean" and Replace everywhere on "Joker"
            Dim regex As New Regex("Bean", RegexOptions.IgnoreCase)

            ' Start:

            ' Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            For Each item As ContentRange In dc.Content.Find(regex).Reverse()
                item.Replace("Joker", New CharacterFormat() With {
                .BackgroundColor = Color.Yellow,
                .FontName = "Arial",
                .Size = 16.0
                   })
            Next item

            ' End:

            ' The code above finds and replaces the content in the whole document.
            ' Let us say, you want to replace a text inside shape blocks only:

            ' 1. Comment the code above from the line "Start" to the "End".
            ' 2. Uncomment this code:
            'For Each shp As Shape In dc.GetChildElements(True, ElementType.Shape).Reverse()
            'For Each item As ContentRange In shp.Content.Find(regex).Reverse()
            'item.Replace("Joker", New CharacterFormat() With {
            '.BackgroundColor = Color.Yellow,
            '.FontName = "Arial",
            '.Size = 16.0
            '       })
            'Next item
            'Next shp

            ' Save the document as DOCX format.
            Dim savePath As String = Path.ChangeExtension(loadPath, ".replaced.docx")
            dc.Save(savePath, SaveOptions.DocxDefault)

            ' Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(loadPath) With {.UseShellExecute = True})
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(savePath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.