I'm trying to do a basic search and replace of text on a .docx word document using OpenXML and Eric White's OpenXmlPowerTools (installed from NuGet). I've followed examples on this site and his blog, but for some reason I never see the changes appearing in the document when I open it after running the code. Here is the simple function I'm running:
void ReadDocument(string path)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
{
var content = doc.MainDocumentPart.GetXDocument().Descendants(W.p);
var regex = new Regex(@"the", RegexOptions.IgnoreCase);
int count = OpenXmlRegex.Replace(content, regex, "NewText", null);
doc.MainDocumentPart.Document.Save();
doc.Save();
MessageBox.Show(count.ToString());
}
}
The message box does show a big number of replacements it should have made, yet when I open the document I see no replacements. Also, I don't think I should need those document .Save() calls, but I've been trying anything to get this thing to work. Any suggestions? Thanks