4

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

Bret Lien
  • 153
  • 2
  • 8

3 Answers3

6

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();
Bret Lien
  • 153
  • 2
  • 8
0

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();               
        }
daniell89
  • 1,832
  • 16
  • 28
  • Thank you for the reply. While this does successfully make a few edits and save the file, and I can see the changes when I open Word, it doesn't work for text that spans multiple Runs in the xml. That's the original issue that led me down the path of trying to use OpenXmlPowerTools, which can supposedly do a text search for text that spans multiple runs. Your approach is not unlike the other one I have tried based on the accepted answer in this question: http://stackoverflow.com/questions/28697701/openxml-tag-search – Bret Lien Mar 21 '17 at 17:08
0

A simpler solution for just replacing text using PowerTools is as follows:

void ReadDocument(string path)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
    {
        TextReplacer.SearchAndReplace(doc, "the", "New text", true);
    }
}
Richard
  • 439
  • 3
  • 25