---
title: "How can I find and replace text in .docx (Microsoft word file) using Interop?"  
description: "How can I find and replace text in .docx (Microsoft word file) using Interop?"  
author: "Steilla Mitchel"  
published: 2023-07-19  
updated: 2023-07-19  
canonical: https://www.mindstick.com/forum/159152/how-can-i-find-and-replace-text-in-docx-microsoft-word-file-using-interop  
category: "ms office"  
tags: ["ms office"]  
reading_time: 3 minutes  

---

# How can I find and replace text in .docx (Microsoft word file) using Interop?

How can I find and [replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) in .docx ([Microsoft](https://www.mindstick.com/articles/12301/microsoft-is-trying-to-kill-passwords) [Word file](https://www.mindstick.com/forum/23364/display-word-file-in-same-format-in-textarea)) using [Interop](https://www.mindstick.com/forum/159638/getting-error-microsoft-office-interop-outlook-doesn-t-work)?

## Replies

### Reply by Aryan Kumar

Sure, here is the code to find and replace text in a .docx document using interop:

C#

```plaintext
using System;
using System.IO;
using System.Runtime.InteropServices;

public class FindAndReplace
{
    private const string FindWhat = "find this text";
    private const string ReplaceWith = "replace with this text";

    public static void Main()
    {
        // Get the path to the .docx document
        string filePath = @"c:\temp\mydocument.docx";

        // Create a new Word object
        object word = CreateObject("Word.Application");

        // Get the active document
        object document = word.ActiveDocument;

        // Find the text
        object range = document.Range;
        range.Find(FindWhat, ReplaceWith, 1, 1);

        // Close the Word object
        word.Quit();
    }
}
```

This code first declares a couple of constants:

- `FindWhat` is the text you want to find.
- `ReplaceWith` is the text you want to replace `FindWhat` with.

The code then gets the path to the .docx document from the user. It then creates a new [Word](https://www.mindstick.com/forum/305/read-word-file) object. It then gets the active document from the Word object.

The code then finds the text using the `Find` method. The `Find` method takes four arguments:

- The text you want to find.
- The text you want to replace the find what text with.
- A flag that indicates whether or not you want to match the case of the find what text.
- A flag that indicates whether or not you want to match the whole word.

In this case, we are setting the `matchCase` and `matchWholeWord` flags to 1. This means that the `Find` method will only match the find what text if it is in the same case and if it is a whole word.

If the find what text is found, the code closes the Word object.

To use this code, you would first need to compile it into an assembly. You would then need to run the assembly. When running the assembly, you would be prompted to enter the path to the .docx document. Once you have entered the path to the .docx document, the code will find and replace the text in the document.

Here are some additional things to keep in mind about finding and replacing text in .docx documents using interop:

- You can use the `FindText` method to find the text.
- You can use the `ReplaceText` method to replace the text.
- You can use the `MatchCase` and `MatchWholeWord` flags to control how the text is matched.
- You can use the `ReplaceAll` method to replace all instances of the find what text with the replace with text.


---

Original Source: https://www.mindstick.com/forum/159152/how-can-i-find-and-replace-text-in-docx-microsoft-word-file-using-interop

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
