Silva
Silva

Reputation: 675

Replacing data in word using DocumentFormat.OpenXml

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.

Upvotes: 0

Views: 472

Answers (1)

lwin
lwin

Reputation: 4460

Just Change=>

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

To

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

Upvotes: 1

Related Questions