开发者

Word2007: Access Violation Exception was unhandled when execute Selection.Find.Execute

开发者 https://www.devze.com 2022-12-17 10:16 出处:网络
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 propose

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 开发者_JS百科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?


Is the problem consistent on the machines with the exception, or intermittent?

If consistent, I don't know the solution, but first check to see if those machines have UAC turned OFF (i.e., the thing added to Vista that prompts you when it thinks something is trying to change your PC) (or opposite from the working ones).

Related to this, you can get security access violations like this when using COM, when trying to talk from an unpriveledged app to a priveledged one (e.g., started "As Administrator"). E.g., if someone started your app As Administrator (perhaps even from VS running As Administrator), and Word was already running NOT As Administrator, then Windows won't be able to start another instance of Word, and won't let the two talk, so the COM will fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消