Find and Replace Text in Documents

[ ]

Use one of the Replace methods to find or replace a particular string within the current range. It returns the number of replacements made, so it is useful for searching strings without replace. An exception is thrown if a captured or replacement string contains one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, footnote.

The following code example shows how to find some text and replace it:

Document doc = new Document("../../data/document.doc");

// Replaces all 'sad' and 'mad' occurrences with 'bad'.
doc.Range.Replace("document", "document replaced", false, true);

// Replaces all 'sad' and 'mad' occurrences with 'bad'.
doc.Range.Replace(new Regex("[s|m]ad"), "bad");

See also: