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.
How to Replace Text in a Word Document
- Download a C# library for replacing words in a Word Document
- Load an existing Word Document
- Access the Paragraphs and Texts of the Word Document
- Replace the old text with new text using
ReplaceText
- Save the updated Word document
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.
ReplaceText
method is case-sensitive and replaces all instances of the string within the selected paragraph.Input
In this example, we'll be using this sample Word document that contains two paragraphs, both with the text "old text".

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
Output

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.
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
Output

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
Output

As you can see, it returned the matching text.
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.