Find and replace in word docs


#1

I need to be able to search through 172,000 Word documents within a folder. Then search for a string within each document, find and replace one string in the document with another. Can you please provide me with a code sample? I work for an organization with very high security and GitHub is blocked, so I am not able to download the examples.


#2

@nsanoir,

You can use the following code to parse .doc/.docx files in a directory and then search string inside each document.

ArrayList list = new ArrayList();
string[] fileNames = Directory.GetFiles("E:\\Temp\\", "*.doc?", SearchOption.TopDirectoryOnly);
foreach (string fileName in fileNames)
{
    Document doc = new Document(fileName);
    // find/detect keyword in Word document
    int count = doc.Range.Replace("pattern", "pattern");
    if (count > 0)
    {
        // Count this document
        list.Add(fileName);
    }
} 

Hope, this will help you in achieving what you are looking for.