c#Saving&当多个实例/文件一次打开时关闭office文档

时间:2017-09-13 13:20:33

标签: c# .net winforms visual-studio ms-word

我正在开发一个文件备份程序,可自动保存所有打开的办公文档。目前,我正在与WORD合作并陷入困境。只要只有一个对话框出现,我就可以成功保存并关闭一个活动的单词实例,而不会出现任何对话框。如果我有多个word文档打开,第二个文档在第一个文档关闭时得到一个另存为对话框。有谁知道如何绕过这个或者是否有可能?

保存和关闭代码,

 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);
                    }

                }

return true;

2 个答案:

答案 0 :(得分: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);
                    }

                }

答案 1 :(得分:0)

您需要在document循环中使用oWordApp.ActiveDocument而不是foreach

所以,例如,而不是:

oWordApp.ActiveDocument.Close

你应该使用:

document.Close