2017-09-13 79 views
0

我正在開發一個文件備份程序,該程序可以自動保存所有打開的辦公文檔。目前我正在和WORD合作,並陷入困境。只要只有一個正在運行,我就可以成功保存並關閉一個活動的單詞實例,而不顯示任何對話框。如果我打開了多個單詞文檔,第一個文檔在第一個文檔關閉時將獲得另存爲對話框。有誰知道我怎麼能解決這個問題,或者如果可能的話?c#保存並關閉辦公文檔,當一次打開多個實例/文件時

代碼保存和關閉,

using Microsoft.Office.Interop.Word; 

    public static bool WordClass1(string doc,string sloc) 

     { 

      if (System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") != null) 

      { 

       Object oMissing = System.Reflection.Missing.Value; 

       Object oTrue = true; 

       Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); 

       int i = 0; 
       i++; 
       string num = i.ToString(); 
       Object oSaveAsFileWord = sloc; 
       foreach (Microsoft.Office.Interop.Word.Document document in oWordApp.Documents) 
       { 
        if (string.Equals(document.Name, doc)) 
        { 
         Console.WriteLine("Found Document"); 





         document.SaveAs(ref oSaveAsFileWord, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing); 



         object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 

         oWordApp.ActiveDocument.Close(ref doNotSaveChanges, ref oMissing, ref oMissing); 
        } 

       } 

迴歸真實;

+0

爲什麼你使用'oWordApp.ActiveDocument'而不是'document'你'foreach'循環內? – mjwills

+0

那麼由於某種原因,往往工作,原來的代碼我發佈的問題是oWordApp.Quit(ref oMissing,ref oMissing,ref oMissing); 我正在嘗試關閉所有文字,然後將其保存到下一個文件。愚蠢的錯誤。 – user2190928

回答

0
public static bool WordClass1(string doc,string sloc) 

     { 

      if (System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") != null) 

      { 

       Object oMissing = System.Reflection.Missing.Value; 

       Object oTrue = true; 

       Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"); 

       int i = 0; 
       i++; 
       string num = i.ToString(); 
       Object oSaveAsFileWord = sloc; 
       foreach (Microsoft.Office.Interop.Word.Document document in oWordApp.Documents) 
       { 
        if (string.Equals(document.Name, doc)) 
        { 
         Console.WriteLine("Found Document"); 





         document.SaveAs(ref oSaveAsFileWord, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
         ref oMissing, ref oMissing, ref oMissing, ref oMissing); 



         object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 

         oWordApp.Document.Close(ref doNotSaveChanges, ref oMissing, ref oMissing); 
        } 

       } 
+0

你可能希望在這裏添加一條評論關於**你改變了什麼**。代碼塊(沒有一些評論)傾向於低估... – mjwills

0

您需要使用document而不是oWordApp.ActiveDocumentforeach循環內。

因此,例如,而不是:

oWordApp.ActiveDocument.Close 

你應該使用:

document.Close