我讀了很多關於這個可怕而令人討厭的錯誤的帖子:「訪問衝突異常未處理」。但是Word2007中只有一篇文章和提升命令「Selection.Find.Execute」。建議的解決方案是重新安裝Word2007,這是一個小小的瑣事!Word2007:執行Selection.Find.Execute時出現訪問衝突異常未處理
我試圖解釋我的環境。
我有一個應用程序用C#寫成,使用WinForms並通過InteropServices(word.dll)調用Word在Word中創建新文檔(引發Word2007實例)。所有代碼都在.NET Framework 3.5上運行。在C#代碼中,我創建了文檔,然後搜索了一些東西來代替它。
只有在某些機器上,程序會在其他正確運行的機器上引發引用的錯誤。
這是代碼段:
WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
WordApp.Visible = false;
...
this.copDoc = WordApp.Documents.Add(ref template,
ref missing,
ref missing,
ref isVisible);
this.copDoc.Activate();
...
WordApp.Selection.Find.Execute(ref textData, 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);
if (WordApp.Selection.Find.Found)
{
Range r = WordApp.Selection.Range;
r.Text = DateTime.Today.ToLongDateString();
}
...
當代碼以粗體它會引發與消息「試圖讀取或寫入受保護的存儲器中的「訪問衝突」異常執行線這通常是指示其他存儲器腐敗的 。」。如果我評論這段代碼,那麼這個文檔就已經創建了,這可能意味着這個例外與整個Word沒有關係,只是在它的一些特別的配置上。
這裏是個例外:
'EdAg.exe': Loaded 'C:\Windows\assembly\GAC\Microsoft.Office.Interop.Word\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll', Binary was not built with debug information.
'EdAg.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\Microsoft.Office.Interop.Word\12.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll'
First-chance exception at 0x45c7a6f3 in EdAg.exe: 0xC0000005: Access violation.
First-chance exception at 0x6b815095 in EdAg.exe: 0xC0000005: Access violation reading location 0x45c7a6f3.
First-chance exception at 0x6b815095 in EdAg.exe: 0xC0000005: Access violation reading location 0x45c7a6f3.
可能有人建議我一個更合理的方式,而不是重新安裝Word 2007中?
您的解決方案非常有趣,但現在我已經解決了修復Word 2007安裝的問題。我會在未來的時間得到你告訴我的解決方案。謝謝 – robob 2010-01-23 16:58:10