I have added new overloads to Paragraph.ReplaceText and DocX.ReplaceText. These new overloads allows you to specify
1) A new formatting,
2) A match formatting,
Below is an example of this new functionality in action. I will be adding this to DocX version 220.127.116.11. I want to add more before releasing a new version, so if you would like to test this functionality before release, please send me an email.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
// The formatting to match.
Formatting matchFormatting = new Formatting();
matchFormatting.Size = 10;
matchFormatting.Italic = true;
matchFormatting.FontFamily = new FontFamily("Times New Roman");
// The formatting to apply to the inserted text.
Formatting newFormatting = new Formatting();
newFormatting.Size = 22;
newFormatting.UnderlineStyle = UnderlineStyle.dotted;
newFormatting.Bold = true;
// Loop through the paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
* Replace all instances of the string "wrong" with the string "right" and ignore case.
* Each inserted instance of the string "new" should use the Formatting newFormatting.
* Only replace an instance of "wrong" if it is Size 10, Italic and Times New Roman.
* SubsetMatch means that the formatting must contain all elements of the match formatting,
* but it can also contain additional formatting for example Color, UnderlineStyle, etc.
* ExactMatch means it must not contain additional formatting.
p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase, newFormatting, matchFormatting, MatchFormattingOptions.SubsetMatch);
// Save all changes made to this document.
}// Release this document from memory.