System.AccessViolationException:尝试读取或写入受保护的内存

发布于 2024-08-30 12:15:25 字数 2233 浏览 2 评论 0原文

当我尝试在 Windows Vista 或 Windows 7 上运行的 Word 2007 中“查找和替换”时,出现以下异常。

System.AccessViolationException: 试图读或写保护 记忆。这通常是一个迹象 其他内存已损坏。在 Microsoft.Office.Interop.Word.Find.Execute(对象& 查找文本、对象和匹配大小写、对象和 匹配整个单词、对象和 匹配通配符、对象和 MatchSoundsLike、Object& MatchAllWordForms、对象和向前, 对象&换行、对象&格式、对象和 替换为、对象&替换、对象& MatchKashida、Object&匹配变音符号, 对象& MatchAlefHamza、Object& 匹配控制)

有什么解决方案吗?

我正在使用.NET3.5 C#。

**********代码****************

public static Application Open(string fileName)
{
    object fileNameAsObject = (object)fileName;
    Application wordApplication;            
    wordApplication = new Application();
    object readnly = false;
    object missing = System.Reflection.Missing.Value;
    wordApplication.Documents.Open(
        ref fileNameAsObject, ref missing, ref readnly, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing
    );

    return wordApplication;             
}

private static void ReplaceObject(
    ref Application wordApplication, 
    object ObjectTobeReplaced, object NewObject)
{
    // ++++++++Find Replace options Starts++++++

object findtext = ObjectTobeReplaced;
    object findreplacement = NewObject;
    object findforward = true;
    object findformat = false;
    object findwrap = WdFindWrap.wdFindContinue;
    object findmatchcase = false;
    object findmatchwholeword = false;
    object findmatchwildcards = false;
    object findmatchsoundslike = false;
    object findmatchallwordforms = false;
    object replace = 2; //find = 1; replace = 2
    object nevim = false;
    Range range = wordApplication.ActiveDocument.Content;
    range.Find.Execute(
        ref findtext, ref findmatchcase, ref findmatchwholeword, 
        ref findmatchwildcards,ref findmatchsoundslike, 
        ref findmatchallwordforms, ref findforward, ref findwrap,
        ref findformat, ref findreplacement, ref replace, 
        ref nevim, ref nevim, ref nevim, ref nevim
        );

I get the following exception when I try to "find and replace" in a Word 2007 working on Windows Vista or Windows 7.

System.AccessViolationException:
Attempted to read or write protected
memory. This is often an indication
that other memory is corrupt. at
Microsoft.Office.Interop.Word.Find.Execute(Object&
FindText, Object& MatchCase, Object&
MatchWholeWord, Object&
MatchWildcards, Object&
MatchSoundsLike, Object&
MatchAllWordForms, Object& Forward,
Object& Wrap, Object& Format, Object&
ReplaceWith, Object& Replace, Object&
MatchKashida, Object& MatchDiacritics,
Object& MatchAlefHamza, Object&
MatchControl)

Is there any solution for this ?

Iam using .NET3.5 C#.

**********CODE****************

public static Application Open(string fileName)
{
    object fileNameAsObject = (object)fileName;
    Application wordApplication;            
    wordApplication = new Application();
    object readnly = false;
    object missing = System.Reflection.Missing.Value;
    wordApplication.Documents.Open(
        ref fileNameAsObject, ref missing, ref readnly, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing,ref missing, ref missing, ref missing, 
        ref missing
    );

    return wordApplication;             
}

private static void ReplaceObject(
    ref Application wordApplication, 
    object ObjectTobeReplaced, object NewObject)
{
    // ++++++++Find Replace options Starts++++++

object findtext = ObjectTobeReplaced;
    object findreplacement = NewObject;
    object findforward = true;
    object findformat = false;
    object findwrap = WdFindWrap.wdFindContinue;
    object findmatchcase = false;
    object findmatchwholeword = false;
    object findmatchwildcards = false;
    object findmatchsoundslike = false;
    object findmatchallwordforms = false;
    object replace = 2; //find = 1; replace = 2
    object nevim = false;
    Range range = wordApplication.ActiveDocument.Content;
    range.Find.Execute(
        ref findtext, ref findmatchcase, ref findmatchwholeword, 
        ref findmatchwildcards,ref findmatchsoundslike, 
        ref findmatchallwordforms, ref findforward, ref findwrap,
        ref findformat, ref findreplacement, ref replace, 
        ref nevim, ref nevim, ref nevim, ref nevim
        );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-09-06 12:15:25

重新安装 Office 后问题得到解决:)...但仍然不知道是什么导致了问题

Issue was fixed after reinstalling Office :)... But still dont know what caused the issue

无人接听 2024-09-06 12:15:25

如果您不显示任何代码,这几乎是不可能回答的。

一个(疯狂的)猜测是,您在与未初始化的 Word 进行互操作时提供了一些类/结构,然后 Word 尝试访问未初始化的内存。

This is next to impossible to answer without you showing any code you have.

A (wild) guess would be that you supply some class/structure when interoping with Word that is not initialized and Word then tries to access the uninitialized memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文