当我尝试在 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
);