簡體   English   中英

嘗試在C#應用程序中關閉活動的Word文檔時出錯

[英]Error when trying to close an active Word document in c# application

我在c#和.net 4.0 Framework和Office 2010中使用

我目前正在嘗試弄清楚如何關閉一個已經打開的Word文檔。 我在使用此代碼時遇到錯誤:

Microsoft.Office.Interop.Word.Application app = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
        foreach (Microsoft.Office.Interop.Word.Document d in app.Documents)
        {
            if (d.FullName.ToLower() == "OOPOR".ToLower())
            {
                object saveOption = WdSaveOptions.wdPromptToSaveChanges;
                object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
                object routeDocument = false;
                d.Application.Documents.Close(ref saveOption, ref originalFormat, ref routeDocument);

            }
        }

當前,當我運行此代碼時,我無法進入foreach循環。 app.Documents顯示計數為“ 0”。 如果我在foreach循環上設置一個斷點並查看'app'變量,它將指出:

ActiveDocument = '((Microsoft.Office.Interop.Word.ApplicationClass)(app)).ActiveDocument' threw an exception of type 'System.Runtime.InteropServices.COMException' >> 
base {System.Runtime.InteropServices.ExternalException} = {"This command is not available because no document is open."}

即使我有一個活動的Word文檔,也會發生這種情況。

看來我的問題源於用戶與管理員的沖突。

以提升的特權運行的應用程序無法看到Word.Application及其文檔以標准用戶身份運行。

暫無
暫無

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

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