2014-02-10 176 views
0

我有一個C#代碼,用於創建word文檔然後關閉它們。 的關閉代碼是這樣的:Word打開時關閉word文檔

// Close and release the Document object. 
if (wordDocument != null) 
{ 
    ((_Document)wordDocument).Close(ref oFalse, ref paramMissing, ref paramMissing); 
    wordDocument = null; 
} 

// Quit Word and release the ApplicationClass object. 
if (wordApplication != null) 
{ 
     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); 
     wordApplication = null; 
} 

的問題是,如果字是在我的電腦已經打開,這個代碼不工作,然後我發現我的計算機上的許多打開的文檔。 有誰知道有什麼問題以及如何解決?

回答

0

我發現有關應用程序實例在這裏的解釋: Process.Kill() issue

「當您啓動Internet Explorer的新實例,它會嘗試看看如果Internet Explorer已經在運行,如果這是真的,URL就在已經運行的實例中打開,新的實例立即退出。「

0

我有同樣的問題,有時文檔或單詞不會關閉。然後我在之後的所有文檔打開時遇到了麻煩。我做的事情是這樣的事情:

try 
{ 
wordDocument.Close(ref nullobject, ref nullobject, ref nullobject); 
} 
catch 
{ 
// do something   
} 
finally 
{ 
wordApplication.Quit(ref nullobject, ref nullobject, ref nullobject); 
} 
+0

不會拋出任何異常,所以它不會幫助,無論如何我關閉了wordApplication。看看我的代碼。 – TamarG

+0

好的,對於這個問題我沒有更多的調查,我轉而使用XML來打開docx,因爲它更簡單,而且我的問題也更少。 – Alois