簡體   English   中英

C#保存Word文檔而沒有提示給出“文檔在其他地方打開錯誤”

[英]C# save word document without prompt give 'document is opened elsewhere error'

我有一個帶有Office Word的Windows窗體。 我正在使用代碼以編程方式打開Word文檔(_wd類型為Microsoft.Office.Interop.Word.ApplicationClass-我正在使用VS 2012,.NET 4.5,Office 2007):

  public void LoadComponent(string path) { if (_wd.Documents.Count != 0) _wd.ActiveDocument.Close(); _wd.Documents.Add(ref path); } 

我想保存活動文檔時出現問題。 我不希望出現保存提示-這是我嘗試過的操作,並且無法取消提示對話框:

_wd.ActiveDocument.Saved = true;
_wd.ActiveDocument.Save();

如果我嘗試:

_wd.ActiveDocument.SaveAs(_filePath,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type。 Type.Missing,Type.Missing,Type.Missing,Type.Missing);

我收到異常:“ Word無法保存此文件,因為該文件已在其他位置打開。” -但是只能在我的應用程序內部打開,而在其他任何地方都不能打開。 如何以編程方式保存不帶保存提示對話框而打開的Word文檔? 有趣的是,即使我嘗試通過按“保存”按鈕來保存文檔,也會出現保存提示對話框->就像以前未在光盤上創建文檔一樣。 但是,文件是從光盤打開的,只有通過編程才能打開。 如果我使用Word打開文檔,然后按“保存”,則不會顯示保存提示對話框。

我收到異常:“ Word無法保存此文件,因為該文件已在其他位置打開。”

僅當在Application另一個實例中打開文檔時,才應收到此Application

以下示例對我有用,並且不提示:

var path = @"C:\Temp\temp.docx";

Word.Application wordApp = new Word.Application();
Word.Documents documents = wordApp.Documents;
Word.Document doc = documents.Add(path);
doc.Range().Text = "Test Text";
doc.SaveAs2(path);
wordApp.Quit();

在任務管理器中檢查WINWORD.EXE進程的雜散實例。 如果Application實例未正確關閉(以編程方式),則可能發生它們。

這些雜散過程之一可能是使Document保持打開狀態。

似乎您不是唯一一個擁有它的人。 可能是MSWORD中的錯誤。 這里的建議

·         Click on start->run.
·         Type %appdata% and click ok.
·         Go to Microsoft->templates folder and delete Normal.dotm.
·         Restart word

暫無
暫無

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

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