簡體   English   中英

一次打開多個實例/文件時,c#保存和關閉辦公文檔

[英]c# Saving & Closing office documents, when multiple instances/files open at once

我正在研究一種文件備份程序,該程序可以自動保存所有打開的Office文檔。 目前,我正在使用WORD,但遇到了麻煩。 只要只運行一個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);
                    }

                }

返回true;

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

                }

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

因此,例如,而不是:

oWordApp.ActiveDocument.Close

您應該使用:

document.Close

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM