3
votes

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

2

2 Answers

5
votes

I very luckily stumbled across the answer at 18:52 into the OpenXmlRegex youtube video (https://youtu.be/rDGL-i5zRdk?t=18m52s).. I need to call this PutXDocument() method on the MainDocumentPart for the changes to go into effect (instead of the doc.Save() I was trying to do)

doc.MainDocumentPart.PutXDocument();
0
votes

Try this approach:

        using (WordprocessingDocument doc = WordprocessingDocument.Open(@"filePath", true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }

            Regex regexText = new Regex(@"the", RegexOptions.IgnoreCase);
            docText = regexText.Replace(docText, "New text");

            using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }

            doc.MainDocumentPart.Document.Save();               
        }