Replace Word Text with Another One with C#/VB.NET

Why do we want to find a specified word in a document? One reason is to confirm its location or highlight it to tell readers that it is important. The other reason is that to replace it with other word.

We will check our article or announcement several times after completing. Sometimes, we need to correct wrong spellings or use more appropriate words in one sentence. At that time, replace function can help us a lot to correct or replace words quickly.

In this post, I will introduce a method about how to replace words in MS Word with C#/VB.NET. And I use a .NET Word Component, Spire.Doc for .NET to help me realize this function more easily.

In my example, I will replace all the word “grown-ups” with “children” of this document.

Detailed Step Shown as Following:

  1. Load the document which I want to replace word from my computer.
  2. Replace word. document.Replace method is used. I pass four parameters to this method, word I want to replace, new word, bool value for case sensitive and bool value to confirm if replacement will be applied for whole word.
  3. Save and launch file.

Main Coding:


using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace ReplaceWord
    class Replace
        static void Main(string[] args)
            //Load File
            Document document = new Document();
            document.LoadFromFile(@”D:\work\My Documents\Little Prince.docx”);

            document.Replace(“grown-ups”, “children”, true, true);

            //Save and Launch
            document.SaveToFile(“Replace.docx”, FileFormat.Docx);


Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace ReplaceWord
    Friend Class Replace
        Shared Sub Main(ByVal args() As String)
            ‘Load File
            Dim document As New Document()
            document.LoadFromFile(“D:\work\My Documents\Little Prince.docx”)

            document.Replace(“grown-ups”, “children”, True, True)

            ‘Save and Launch
            document.SaveToFile(“Replace.docx”, FileFormat.Docx)
        End Sub
    End Class
End Namespace

Result Shown as Following:


                            Original Document                                                                       Result Document

Download Spire.Doc for .NET Here


2 thoughts on “Replace Word Text with Another One with C#/VB.NET

  1. Pingback: My Homepage

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s