0

I'm working in C# (.NET, WPF, MS VS 2013) with Microsoft.Office.Interop.Word. I open some MS word document. I change text and I save the result. But the problem is that the format of document (mainly font size) is changed. For example:

Hello World! (bold, font size 14)

Say Hello World! (regular, font size 12)

after changing and saving:

Hello World! (bold, font size 14)

Do not Say Hello World! (bold, font size 14)

Where make I mistake? Here is my code:

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(FileName);

doc.Content.Text = Regex.Replace(doc.Content.Text, "Say", "Do not Say", RegexOptions.IgnoreCase);

FileName = FileName.Replace(".", "_new.");

object fileName = FileName;

doc.SaveAs2(ref fileName);
Community
  • 1
  • 1
Jan Nowak
  • 43
  • 8
  • If you want to preserve formatting you **have** to use Word's own "Find and Replace" functionality. If you do it otherwise, like in your case using a Regex, formatting is lost. – stuartd Jun 21 '17 at 12:52
  • Thank you for explanation, I have finally find good solution for me on stackoverflow: [link](https://stackoverflow.com/questions/19252252/c-sharp-word-interop-find-and-replace-everything) – Jan Nowak Jun 22 '17 at 07:46

0 Answers0