2017-09-13 6 views
0

オープンしているオフィス文書を自動的に保存するファイルバックアッププログラムを作成しています。現時点では私はWORDと一緒に仕事をしています。 1つしか実行されていない限り、ダイアログの表示なしで、単語のアクティブなインスタンスを正常に保存して閉じることができます。複数のWord文書を開いている場合、最初の文書を閉じるときに2番目の文書には名前を付けて保存ダイアログが表示されます。誰かがこれをどうやってやっていくことができるのか、それが可能なら知っていますか?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); 
        } 

       } 

リターンはtrue。

+0

をなぜあなたは 'oWordApp.ActiveDocument'ではなく'あなた 'foreach'ループ内document'使うのですか? – 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

あなたのforeachループ内documentではなくoWordApp.ActiveDocumentを使用する必要があります。

したがって、たとえば、ではなく:

oWordApp.ActiveDocument.Close 

あなたが使用する必要があります。

document.Close 
関連する問題