In general, I make a report with the replacement of certain words with specific meanings ... It is necessary that the changes in the file remain ... But for some reason, the limited functionality mode works for me ... In what methods, in what parameters is this put? Work in .doc and .docx formats
private void button4_Click(object sender, EventArgs e) { //Создаём новый Word.Application Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); //Загружаем документ Microsoft.Office.Interop.Word.Document doc = null; object fileName = "E:\\R.doc"; object falseValue = false; object trueValue = true; object missing = Type.Missing; doc = app.Documents.Open(ref fileName, ref missing, ref trueValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); //Теперь у нас есть документ который мы будем менять. //Очищаем параметры поиска app.Selection.Find.ClearFormatting(); app.Selection.Find.Replacement.ClearFormatting(); //Задаём параметры замены и выполняем замену варианта применеия. object findText = "<VARIANT>"; object replaceWith = "на что меняем"; object replace = 2; app.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceWith, ref replace, ref missing, ref missing, ref missing, ref missing); app.Visible = true; app.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; doc.Saved = true; //doc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges); //app.Quit(false); }
ref? After all, to open for example, you can simply useapp.Documents.Open("Путь");this is hundreds of times more beautiful and more comfortable! - EvgeniyZ