2017-09-13 72 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