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.
Quickstart: Replace Text in Word Documents Easily
This quick guide demonstrates how to swiftly replace text in a Word document using IronWord. With just a few lines of C#, you can load a DOCX file, replace specified text, and save the updated document. This efficient process ensures that developers can enhance document automation without the need for Microsoft Office Interop, making it ideal for backend processes and web applications.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
IronWord.Document doc = new IronWord.Document("sample.docx"); doc.ReplaceText("oldText", "newText"); doc.SaveAs("updated.docx");Deploy to test on your live environment
Minimal Workflow (5 steps)
- 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.csusing 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.comOutput

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.csusing 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.comOutput

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.csusing 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.comOutput

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.Frequently Asked Questions
What is IronWord used for?
IronWord is a C# library used for automating text replacement in Word documents. It simplifies tasks such as personalizing templates, updating reports, and managing content in bulk.
Do I need Microsoft Word installed to use IronWord?
No, IronWord does not rely on Microsoft Office Interop, so you don't need Word installed on your server. This makes it a faster and more scalable solution for manipulating Word documents.
How do I replace text in a Word document using IronWord?
To replace text in a Word document using IronWord, load the document, access the paragraphs, and use the ReplaceText method to substitute old text with new text.
Is the ReplaceText method in IronWord case-sensitive?
Yes, the ReplaceText method in IronWord is case-sensitive and replaces all instances of the specified string within the selected paragraph.
Can I replace text across the entire document with IronWord?
Yes, you can replace text throughout the entire document by looping through the Paragraphs collection and applying the ReplaceText method to each paragraph.
What happens if the text to be replaced is not found in a paragraph?
If the text is not found in a paragraph, no operation is performed, and the loop continues to the next paragraph.
How can I verify if a specific text exists in a document using IronWord?
You can use the FindText method to search the entire document and return the first TextElement that matches the search query.
What should I do if FindText returns nothing?
If FindText returns nothing, it means 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.




