2

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

System.AccessViolationException:试图读取或写入受保护的内存。这通常表明其他内存已损坏。在 Microsoft.Office.Interop.Word.Find.Execute(对象和 FindText、对象和 MatchCase、对象和 MatchWholeWord、对象和 MatchWildcards、对象和 MatchSoundsLike、对象和 MatchAllWordForms、对象和转发、对象和换行、对象和格式、对象和替换、对象和替换、对象和 MatchKashida、对象和MatchDiacritics、对象和 MatchAlefHamza、对象和 MatchControl)

有什么解决方案吗?

我正在使用 .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
        );
4

2 回答 2

2

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

于 2010-04-30T13:03:34.837 回答
1

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

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

于 2010-04-30T12:11:30.720 回答