I am using Open XML SDK to highlight a specific word inside a docx file but I not able to do that, after an extensive research I did the following I tried to open the document and then edit the color of the word and save it again, but I get no thing saved while I found the document last edit time with now date.
What is wrong with that code?
void HighLightWord(string documentUrl, string word)
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(documentUrl, true))
{
var body = wordDoc.MainDocumentPart.Document.Body;
var paras = body.Elements<Paragraph>();
DocumentFormat.OpenXml.Wordprocessing.Color color = new DocumentFormat.OpenXml.Wordprocessing.Color();
foreach (var para in paras)
{
foreach (var run in para.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
if (text.Text.Contains(word))
{
color.Val = "365F91";
run.Append(color);
wordDoc.MainDocumentPart.Document.Save();
return;
}
}
}
}
wordDoc.Close(); // close the template file
}
}