我有一個WinForms應用程序,我正在使用Word Automation通過模板構建文檔,然後將它們保存到數據庫。在創建文檔後,我從數據庫檢索文檔,將其寫入臨時目錄中的文件系統,然後使用Word Interop服務打開文檔。Word Automation - 文件正在被其他應用程序或用戶使用
有一個加載的文檔列表,用戶只能打開每個文檔的一個實例,但可以同時打開多個不同的文檔。我沒有與打開,保存,和關閉任何問題,當他們打開1號文件,然而,當他們同時打開多個文檔,我關閉所有我的Word的實例時收到以下錯誤:
The file is in use by another application or user. (C:\...\Templates\Normal.dotm)
This error is commonly encountered when a read lock is set on the file that you are attempting to open.
我使用下面的代碼來打開文檔和處理BeforeDocumentClosed事件:
public void OpenDocument(string filePath, Protocol protocol, string docTitle, byte[] document)
{
_protocol = protocol;
documentTitle = docTitle;
_path = filePath;
if (!_wordDocuments.ContainsKey(_path))
{
FileUtility.WriteToFileSystem(filePath, document);
Word.Application wordApplication = new Word.Application();
wordApplication.DocumentBeforeClose += WordApplicationDocumentBeforeClose;
wordApplication.Documents.Open(_path);
_wordDocuments.Add(_path, wordApplication);
}
_wordApplication = _wordDocuments[_path];
_currentWordDocument = _wordApplication.ActiveDocument;
ShowWordApplication();
}
public void ShowWordApplication()
{
if (_wordApplication != null)
{
_wordApplication.Visible = true;
_wordApplication.Activate();
_wordApplication.ActiveWindow.SetFocus();
}
}
private void WordApplicationDocumentBeforeClose(Document doc, ref bool cancel)
{
if (!_currentWordDocument.Saved)
{
DialogResult dr = MessageHandler.ShowConfirmationYnc(String.Format(Strings.DocumentNotSavedMsg, _documentTitle), Strings.DocumentNotSavedCaption);
switch (dr)
{
case DialogResult.Yes:
SaveDocument(_path);
break;
case DialogResult.Cancel:
cancel = true;
return;
}
}
try
{
if (_currentWordDocument != null)
_currentWordDocument.Close();
}
finally
{
Cleanup();
}
}
public void Cleanup()
{
if (_currentWordDocument != null)
while(Marshal.ReleaseComObject(_currentWordDocument) > 0);
if (_wordApplication != null)
{
_wordApplication.Quit();
while (Marshal.ReleaseComObject(_wordApplication) > 0);
_wordDocuments.Remove(_path);
}
}
有誰看到什麼問題,我做的,允許在同一時間多個文檔的開放?我對Word自動化和Word Interop服務相當陌生,所以有任何建議。謝謝。
你直接在你的代碼打開鎖定的文件(C:\ ... \模板\的Normal.dotm),或者該自動訪問? 如果你是打開它的人,你可以創建一個副本或類似的東西,以避免鎖定,或嘗試只讀訪問它。 – 2010-12-06 14:02:06