I read a lot of posts on this terrible and annoying error: "Access Violation Exception was unhandled ". But only one post on Word2007 and the raising command "Selection.Find.Execute ". The proposed solution was to reinstall Word2007 and it is a little bit trivial!
I try to explain my environment.
I have a little application written in C# using WinForms and calling Word through InteropServices (word.dll) to create new document in Word (raising Word2007 instance). All the code runs on the .NET Framework 3.5. Inside the C# code I create the document and then I search something to substitute in it.
The program, only on some machines, raise the cited error while on the others machines it correctly runs.
this is the snippet code:
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();
}
...
When code executes line in bold it raises the "Access Violation" Exception with message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt .". If I comment this code the document was created and this could mean that the exception is not related on the whole Word but only on some particularly configuration in it.
Here is the exception:
'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.
Could someone suggest me a more rational way instead of reinstalling Word 2007?