none
Attempted to read or write protected memory...at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, RRS feed

  • Question

  • Dear all

       I have develop a winform application in VB.NET, I have Office 2013 installed on my machine, I got following exception in my code

    Here are exception details

    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)
       at clsParagraph_DLL_CSharp.clsParagraph.GenerateParaXML(String ipXMLTemplate, String ipDocFile, String opXMLFile, String id, Boolean bXmChange)

    I have Windows 8 64 Bit OS, till yesterday application was running properly

    Today it gives above error

    How to resolve it

    Thanks

    Koolprasad2003

    Monday, October 5, 2015 9:32 AM

Answers

  • Thanks for the reply cindy, but the problem persist even if I made suggested changes, finally I reinstall my Office 2013 and things are working fine now

    I don't know why this happens, it is really a mysterious exception.

    Thanks for help

    • Marked as answer by koolprasadd Friday, October 9, 2015 6:21 AM
    Friday, October 9, 2015 6:21 AM

All replies

  • What line of code exactly fires the exception? Did you try to debug the code?

    What parameters are passed to the Execute method?

    Monday, October 5, 2015 12:03 PM
  • Have you tried closing all programs and restarting Windows? If it's been running up until now then possibly there's a conflict within Windows that will be resolved by a re-boot.

    If that doesn't help, have you installed any new software or add-ins?

    Otherwise, we require more information about how the WinForm application is interacting with Word...


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 5, 2015 5:00 PM
    Moderator
  • Thanks for reply Cindy, Eugene

    Here is my code snippet

      string szResult = "";
                 word.Application objApp = null;
                 word.Document objDoc = null;
                 object missing = Type.Missing;
                 string szHyperlinkID = "";
                 int iHashIndex = 0, iLastIndex = 0;
    
                 try
                 {
                     string szReadText = "", szText = "";
                     object szFilename = ipDocFile;
                     objApp = new word.Application();
                     objDoc = new word.Document();
    
                     Hashtable Hsh_SpecialChars = new Hashtable();
                    
    
                     objApp.ScreenUpdating = false;
                     objDoc.TrackRevisions = false;
                     objApp.Visible = false;
    
                     Hsh_SpecialChars.Add(" ", "|B");
                     Hsh_SpecialChars.Add(" ", "|DE");
                     Hsh_SpecialChars.Add(" ", "|CFS1");
                     Hsh_SpecialChars.Add(" ", "|PDPP");
    
                     object SaveChanges = word.WdSaveOptions.wdDoNotSaveChanges;
    
                     objDoc = objApp.Documents.Open(ref szFilename,
                          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, ref missing, ref missing);
    
                     word.Range rng;
    
                     object objSpecChar;
                     object wdReplace = word.WdReplace.wdReplaceAll;
                     object ReplaceWith;
    
                     foreach (DictionaryEntry Hsh in Hsh_SpecialChars)
                     {
                         objSpecChar = Hsh.Key;
                         ReplaceWith = Hsh.Value;
    
                         rng =
    objDoc.StoryRanges[Microsoft.Office.Interop.Word.WdStoryType.wdMainTextStory];
    
                         rng.Find.Execute(ref objSpecChar, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
    
    missing, ref ReplaceWith, ref wdReplace, ref missing, ref missing, ref missing, ref missing);
                     }
    
    
                     objDoc.SaveAs(ref fileNameHTML, ref fileFormat, 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, ref missing);
    
                     objDoc.Close(ref missing, ref missing, ref missing);
                     objApp.Quit(ref missing, ref missing, ref missing);
                     Release_Object(objDoc);
                     Release_Object(objApp);
                     objDoc = null;
                     objApp = null;
    

    in above code 'rng.Find.Execute' throws exception as "Attempted to read or write protected memory...at Microsoft.Office.Interop.Word.Find.Execute" I am using Windows 8 64 bit with Office 2013 environment

    Please help me to resolve it

    Thanks

    Prasad

     
    Tuesday, October 6, 2015 12:45 PM
  • Hi Prasad

    << objDoc = new word.Document();>>

    NEVER, NEVER, EVER - under any circumstances - use the "new" keyword with Word.Document. Word won't generate a compile error, but things will NOT function correctly.

    ALWAYS use objDoc = objApp.Documents.Add(); to generate and instantiate a Word.Document object in the Word.Application.

    And ALWAYS set the COM objects to NULL before you release them, in the reverse order you declare and instantiate them:

    objDoc = null;
    objApp = null;


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 6, 2015 5:39 PM
    Moderator
  • Thanks for the reply cindy, but the problem persist even if I made suggested changes, finally I reinstall my Office 2013 and things are working fine now

    I don't know why this happens, it is really a mysterious exception.

    Thanks for help

    • Marked as answer by koolprasadd Friday, October 9, 2015 6:21 AM
    Friday, October 9, 2015 6:21 AM