0

i'm trying to do changes on a word document using DocumentFormat.OpenXml, ive tried even looking the following question: Save modified WordprocessingDocument to new file but i cant find a way to save the change to same file.

Tried the below but the file didn't change.

    public static void WriteToWordDoc(string filepath)
    {
        using (WordprocessingDocument wordprocessingDocument =
             WordprocessingDocument.Open(filepath, true))
        {
            Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
            var old = body.InnerXml.ToString();
            var sdtCont = body.InnerXml.Replace("Hello", "Hi");

          wordprocessingDocument.MainDocumentPart.Document.Save();
          wordprocessingDocument.Close();
        }
    }

Please assist.

Silva
  • 625
  • 1
  • 6
  • 25

1 Answers1

1

Just Change=>

var sdtCont = body.InnerXml.Replace("Hello", "Hi");

To

body.InnerXml=body.InnerXml.Replace("Hello", "Hi");
lwin
  • 3,784
  • 6
  • 25
  • 46