How to Replace Text in a Word Document

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronWord with NuGet Package Manager

    PM > Install-Package IronWord

  2. Copy and run this code snippet.

    IronWord.Document doc = new IronWord.Document("sample.docx");
    doc.ReplaceText("oldText", "newText");
    doc.SaveAs("updated.docx");
  3. Deploy to test on your live environment

    Start using IronWord in your project today with a free trial
    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.

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

提示All 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 theParagraphscollection and apply theReplaceTextmethod 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.

請注意 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.

警告 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.

常見問題解答

IronWord是做什麼用的?

IronWord是一個C#函式庫,用來自動取代Word文件中的文字。它可以簡化諸如個人化模板、更新報告和大量管理內容等任務。

使用 IronWord 需要安裝 Microsoft Word 嗎?

不,IronWord 不依賴 Microsoft Office Interop,因此您的伺服器上無需安裝 Word。這使其成為處理 Word 文件速度更快、可擴展性更強的解決方案。

如何使用 IronWord 取代 Word 文件中的文字?

若要使用 IronWord 取代 Word 文件中的文本,請載入文檔,存取段落,然後使用 ReplaceText 方法將舊文字替換為新文字。

IronWord 中的 ReplaceText 方法區分大小寫嗎?

是的,IronWord 中的 ReplaceText 方法區分大小寫,並將所選段落中所有指定的字串替換為新字串。

我可以使用 IronWord 取代整個文件中的文字嗎?

是的,您可以透過遍歷 Paragraphs 集合並對每個段落應用 ReplaceText 方法來替換整個文件中的文字。

如果要替換的文字在段落中找不到,會發生什麼情況?

如果在段落中找不到該文本,則不執行任何操作,循環繼續到下一個段落。

如何使用 IronWord 驗證文件中是否存在特定文字?

您可以使用 FindText 方法搜尋整個文檔,並傳回與搜尋查詢相符的第一個 TextElement。

如果 FindText 沒有回傳任何結果,我該怎麼辦?

如果 FindText 函數沒有傳回任何結果,則表示文件中不存在該文字。請確保您的搜尋字詞拼字正確,並且大小寫與文件中的文字完全一致。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 25,807 | 版本: 2025.11 剛剛發布