How to Replace Text in a Word Document

Automating text replacement in Word documents is a common need, whether you're personalizing templates, updating reports, or managing content in bulk. IronWord is a C# library designed to make this task straightforward and efficient.

A key advantage of IronWord is that it manipulates Word documents directly, without relying on Microsoft Office Interop. This means you don't need Word installed on your server, resulting in a significantly faster, more reliable, and scalable solution for back-end processes and web applications.

In this how-to guide, we'll walk through code examples to show you just how easily you can find and replace text in any Word document.

Get started with IronWord

Start using IronWord in your project today with a free trial.

First Step:
green arrow pointer


Replace Text Example

Replacing text within a Word Document is intuitive with IronWord. First, we load an existing document. Then, we access the paragraphs collection through Paragraphs and use the ReplaceText method on a specific paragraph.

The ReplaceText method takes two string parameters: the text to find and the text to replace it with.

Tips The ReplaceText method is case-sensitive and replaces all instances of the string within the selected paragraph.

TipsAll object lists used in this example follow zero-based indexing.

Input

In this example, we'll be using this sample Word document that contains two paragraphs, both with the text "old text".

Sample Docx

Code

Here's the code below, we'll be replacing the first paragraph of "old text" with the word "new text".

:path=/static-assets/word/content-code-examples/how-to/replace-words.cs
using IronWord;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Replace the first paragraph's old text with new text
doc.Paragraphs[0].ReplaceText("old text", "new text");

// Save updated Word Document
doc.SaveAs("updated.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Output Docx

As you can see in the output file, only the first paragraph's text has been changed, while the second remains untouched.

Replacing Multiple Text

To replace every occurrence of a word throughout the entire document, loop through the Paragraphs collection and apply the ReplaceText method to each one.

We'll use the same sample.docx file as before. If the text to be replaced isn't found in a paragraph, no operation is performed, and the loop continues to the following paragraph.

Please note If the text you wish to replace is not found, no operations will be performed.

Code

Here's the code below, we'll be replacing the first paragraph of "old text" with the word "new text".

:path=/static-assets/word/content-code-examples/how-to/replace-words-multiple.cs
using IronWord;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Loop through each paragraph to find and replace text
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
    // Replace all occurrences of "old text" with "new text" in the current paragraph
    doc.Paragraphs[i].ReplaceText("old text", "new text");
}

// Save updated Word Document
doc.SaveAs("updated.docx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Output Multiple Docx

As you can see from this updated Word Document, both paragraphs have been replaced with the word "new text".

Find Text

If you need to verify that text exists before performing an operation, you can use the FindText method. This method searches the entire document and returns the first TextElement that matches the search query.

Below is an example of searching for the old text in the Word document mentioned above.

Code

:path=/static-assets/word/content-code-examples/how-to/replace-words-find-text.cs
using IronWord;
using System;

// Open existing Word
WordDocument doc = new WordDocument("sample.docx");

// Returns the first TextContent element that contains the specified text.
Console.WriteLine(doc.FindText("old text"));
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

Find Text Output

As you can see, it returned the matching text.

Warning If FindText returns nothing, the text does not exist in the document. Ensure your search term is spelled correctly and matches the case of the text in the document.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...Read More

Ready to Get Started?
Nuget Downloads 23,662 | Version: 2025.10 just released