Reputation: 675
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
Reputation: 4460
Just Change=>
var sdtCont = body.InnerXml.Replace("Hello", "Hi");
To
body.InnerXml=body.InnerXml.Replace("Hello", "Hi");
Upvotes: 1