public static Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); public static string generalfile = Application.StartupPath + "\\Shablon\\1.docx"; // файл-шаблон public static string newfile = Application.StartupPath + "\\" + Program.namePath + "\\протокол1.docx"; // новый файл на основе файла-шаблона public static Object fileName = generalfile; public static Object missing = Type.Missing; public void OpenFile() { app.Documents.Open(ref fileName); } public void SaveCloseFile() { app.ActiveDocument.SaveAs(newfile); app.ActiveDocument.Close(); app.Quit(); } public void FindReplace(string str_old, string str_new) { Microsoft.Office.Interop.Word.Find find = app.Selection.Find; find.Text = str_old; // текст поиска find.Replacement.Text = str_new; // текст замены find.Execute(FindText: Type.Missing, MatchCase: false, MatchWholeWord: false, MatchWildcards: false, MatchSoundsLike: missing, MatchAllWordForms: false, Forward: true, Wrap: Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue, Format: false, ReplaceWith: missing, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll); } 

When re-creating a file, it swears at the openFile method with the exception System.Runtime.InteropServices.COMException .

The challenge itself:

 OpenFile(); FindReplace("name", Program.namePath); FindReplace("Predsedatel", predsedatel_name + " - " + dolzhnostPred); FindReplace("V1", textBox6.Text.Trim()); FindReplace("V2", textBox7.Text.Trim()); FindReplace("V3", textBox8.Text.Trim()); FindReplace("Com1", comissionname[0] + " - " + dolzhnostCom[0]); FindReplace("Com2", comissionname[1] + " - " + dolzhnostCom[1]); FindReplace("Com3", comissionname[2] + " - " + dolzhnostCom[2]); FindReplace("Com4", comissionname[3] + " - " + dolzhnostCom[3]); FindReplace("Secretar", secretar_name + " - " + dolzhnostSec); FindReplace("ocenkaGos", Program.ocenkaGos + " %"); SaveCloseFile(); 
  • What does "swear" mean? An exception is thrown? Then give the exception text to the question, and InnerException , if any. - VladD
  • Unhandled "System.Runtime.InteropServices.COMException" type exception - Leonid Smirnov
  • Good but not enough. Look for information in the exception object. Message? InnerException? HResult? - VladD
  • one
    @VladD, why did you turn off telepathy?)) - Qwertiy
  • one
    @Qwertiy: Exactly, a crystal ball on recharging :-D - VladD

1 answer 1

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

Once upon a time there was a Word ...

 app.Quit(); 

... but it was closed ...

 app.Documents.Open(ref fileName); 

and then asked to open the file ...
but there was already nobody ...


In general, transfer

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

where the work begins is somewhere before the call to OpenFile .

  • does not work. He changes the same file to me. And I create a folder and upload the file with the changes there - Leonid Smirnov
  • The meaning of the program is to take a file from a folder with a template and write it into a created new folder and write a modified file there. - Leonid Smirnov
  • And after having picked up this line above, everything changes in the first created folder - Leonid Smirnov
  • 2
    Everything works, but I had to remove the static field with new paths. Thank you very much everyone for participating \ - Leonid Smirnov