このひどく迷惑なエラーに関する多くの投稿を読みました:「アクセス違反の例外は処理されませんでした」。ただし、Word2007とレイズコマンド「Selection.Find.Execute」に関する投稿は1つだけです。提案された解決策はWord2007を再インストールすることでした、そしてそれは少し些細なことです!
私は自分の環境を説明しようとしています。
WinFormsを使用してC#で記述された小さなアプリケーションがあり、InteropServices(word.dll)を介してWordを呼び出して、Wordで新しいドキュメントを作成します(Word2007インスタンスを起動します)。すべてのコードは.NETFramework3.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全体に関連しているのではなく、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を再インストールする代わりに、誰かが私にもっと合理的な方法を提案してもらえますか?