Sure, here is the code to find and replace text in a .docx document using interop:
C#
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 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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the code to find and replace text in a .docx document using interop:
C#
This code first declares a couple of constants:
FindWhatis the text you want to find.ReplaceWithis the text you want to replaceFindWhatwith.The code then gets the path to the .docx document from the user. It then creates a new Word object. It then gets the active document from the Word object.
The code then finds the text using the
Findmethod. TheFindmethod takes four arguments:In this case, we are setting the
matchCaseandmatchWholeWordflags to 1. This means that theFindmethod 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:
FindTextmethod to find the text.ReplaceTextmethod to replace the text.MatchCaseandMatchWholeWordflags to control how the text is matched.ReplaceAllmethod to replace all instances of the find what text with the replace with text.