System.AccessViolationException: Attempting to read or write protected memory

I am getting the following exception when I try to "find and replace" in Word 2007 running on Windows Vista or Windows 7.

System.AccessViolationException: An attempt to read or write is protected by Memory. This often indicates that other memory is corrupted. 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 #.

********** THE 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
        );

      

+2


a source to share


2 answers


The problem was fixed after reinstalling Office :) ... But I still don't know what caused the problem.



+2


a source


It's almost impossible to answer if you haven't provided some code that you have.



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

+1


a source







All Articles