locked
Attempted to read or write protected memory. This is often an indication that other memory is corrupt RRS feed

  • Question

  • mailItem.HTMLBody = htmlOr;
                Word.Document doc = mailItem.GetInspector.WordEditor;
                int kk = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);

    {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

    I got it when the  htmlOr is large.

    How can i avoid that ?

    Thanks.

    Monday, July 16, 2012 7:27 PM

Answers

  • First of all, in general you want to avoid using compound dot operators in your managed code. It creates invisible object variables you can't control or release.
     
    In this case you can declare an Inspector object and set it explicitly and also avoid the compound dot operators. When you're done with the Inspector just call its Close() method. Instead of this:
     
        Word.Document doc = mailItem.GetInspector.WordEditor;
     
    Use this:
     
        Outlook.Inspector insp = mailItem.GetInspector;
        Word.Document doc = insp.WordEditor;
     
    I always tell people to avoid trying to get too much out of an Inspector or its CurrentItem before the Activate() event fires for the Inspector. The reason is that in NewInspector() and other places before Activate() fires your Inspector object reference is a "weak" object reference, where not every property is fully filled in. In and after Activate() the reference is a "strong" object reference and all properties are available.
     
    The PM who owns the Outlook object model on the Outlook team once told me that before the object is a strong reference you should only access Inspector.CurrentItem and its MessageClass or Class properties, only what's needed to identify the type of item in the Inspector. Anything else is not guaranteed to be a valid property.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:180508a4-c534-4a46-9d6d-fc4c48d8c090...
    Thanks for your patience. 

    Your code runs without the error.  My mailItem is the current mail, then i set its content with htmOr. I think because it not finished loading yet ( because if error only when i have a large content of htmlOr) when i calculated the number of page. But when i try to make new mail Item ( objMailItem ), it runs fine but looks like stupid because i should close its Inspector after i finish my job. Any solutions for that?

    Word.Document doc = mailItem.GetInspector.WordEditor;            
                int numPage1 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                mailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                objMailItem.HTMLBody = htmlOr;
                objMailItem.GetInspector.Activate();
                doc = objMailItem.GetInspector.WordEditor;           
                int numPage2 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                objMailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                mailItem.GetInspector.Activate();


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Friday, July 20, 2012 4:21 PM
    Thursday, July 19, 2012 1:28 PM

All replies

  • What is your definition of "large"? Does your code work on smaller samples of HTML?
     
    Does this also occur on other machines? What line actually is firing the error?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:0900b2fe-d4af-47f3-9933-2c3d14eaefde...
    mailItem.HTMLBody = htmlOr;
                Word.Document doc = mailItem.GetInspector.WordEditor;
                int kk = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);

    {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

    I got it when the  htmlOr is large.

    How can i avoid that ?

    Thanks.


    Ken Slovak MVP - Outlook
    Tuesday, July 17, 2012 1:17 PM
  • What is your definition of "large"? Does your code work on smaller samples of HTML?
     
    Does this also occur on other machines? What line actually is firing the error?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:0900b2fe-d4af-47f3-9933-2c3d14eaefde...
    mailItem.HTMLBody = htmlOr;
                Word.Document doc = mailItem.GetInspector.WordEditor;
                int kk = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);

    {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

    I got it when the  htmlOr is large.

    How can i avoid that ?

    Thanks.


    Ken Slovak MVP - Outlook

    Thanks for your reply.

    In smaller samples of HTML ( about under 100 lines of content)  it works perfectly . In this line, it fires the exception. 

    int kk = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);


    • Edited by se7enbit Tuesday, July 17, 2012 1:40 PM
    Tuesday, July 17, 2012 1:38 PM
  • What version of Outlook are you on?
     
    I ran this test in the Outlook 2010 VBA project. I selected an email of 283 lines for the test. The VBA code ran without error.
     
    Try setting up an email with the HTML that you want in the user interface and see if it's OK. If it is, select the email and run the test code. See if you ge any exceptions.
     
    Here's the test code. Alt+F11 to open the VBA project (make sure it's enabled), in the project viewer expand Project1\Microsoft Office Outlook\ThisOutlookSession. Select it and paste the test code in the code window. You'll need a reference to Word in the Tools, References dialog:
     
    Sub TestWordStats()
        Dim s As String
        s = Application.ActiveExplorer.Selection.Item(1).HTMLBody
       
        Dim m As Outlook.MailItem
        Set m = Application.CreateItem(olMailItem)
        m.Subject = "test"
        m.HTMLBody = s
        Dim d As Word.document
        Set d = m.GetInspector.WordEditor
        Debug.Print d.Sections(1).Range.ComputeStatistics(wdStatisticPages)
        Debug.Print d.Sections(1).Range.ComputeStatistics(wdStatisticLines)
        Debug.Print d.Sections(1).Range.ComputeStatistics(wdStatisticWords)
    End Sub
     
    I got results of 3, 283, 816 from my run.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:40994ca1-04c2-488e-8901-02f008042c88...
    <snip>
     
    Thanks for your reply.

    In smaller samples of HTML ( about under 100 lines of content)  it works perfectly . In this line, it fires the exception. 

    int kk = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);



    Ken Slovak MVP - Outlook
    Tuesday, July 17, 2012 9:48 PM
  • What version of Outlook are you on?
     

    Ken Slovak MVP - Outlook

    Um.. How can i add the dll? I cannot add the Microsoft.Office.Interop.Word.dll.

    My Outlook is 2007. When i test my code with the mail have about 31 pages in A4 (letter) ( that mean it is about 1000 lines) it fires the exception like that. I think the mail Item has not finished loading yet so that fire the error. But i don not know how to handle with this problem.

     
    Wednesday, July 18, 2012 6:19 PM
  • To add the reference to the Outlook VBA project select Tools, References in the VBA project window. In the References dialog scroll down until you see Microsoft.Word. This is VBA (unmanaged code) and does not use the Interop or CLR.
     
    When do you run your code? Don't run it before the Inspector.Activate() event fires when the item is being opened.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:b58c8a2e-96b7-44ad-a99d-f5df35f880b7...
    Um.. How can i add the dll? I cannot add the Microsoft.Office.Interop.Word.dll.

    My Outlook is 2007. When i test my code with the mail have about 31 pages in A4 (letter) ( that mean it is about 1000 lines) it fires the exception like that. I think the mail Item has not finished loading yet so that fire the error. But i don not know how to handle with this problem.

     

    Ken Slovak MVP - Outlook
    Wednesday, July 18, 2012 10:26 PM
  • To add the reference to the Outlook VBA project select Tools, References in the VBA project window. In the References dialog scroll down until you see Microsoft.Word. This is VBA (unmanaged code) and does not use the Interop or CLR.
     
    When do you run your code? Don't run it before the Inspector.Activate() event fires when the item is being opened.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007

    Ken Slovak MVP - Outlook

    Thanks for your patience. 

    Your code runs without the error.  My mailItem is the current mail, then i set its content with htmOr. I think because it not finished loading yet ( because if error only when i have a large content of htmlOr) when i calculated the number of page. But when i try to make new mail Item ( objMailItem ), it runs fine but looks like stupid because i should close its Inspector after i finish my job. Any solutions for that?

    Word.Document doc = mailItem.GetInspector.WordEditor;            
                int numPage1 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                mailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                objMailItem.HTMLBody = htmlOr;
                objMailItem.GetInspector.Activate();
                doc = objMailItem.GetInspector.WordEditor;           
                int numPage2 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                objMailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                mailItem.GetInspector.Activate();

    Thursday, July 19, 2012 3:02 AM
  • First of all, in general you want to avoid using compound dot operators in your managed code. It creates invisible object variables you can't control or release.
     
    In this case you can declare an Inspector object and set it explicitly and also avoid the compound dot operators. When you're done with the Inspector just call its Close() method. Instead of this:
     
        Word.Document doc = mailItem.GetInspector.WordEditor;
     
    Use this:
     
        Outlook.Inspector insp = mailItem.GetInspector;
        Word.Document doc = insp.WordEditor;
     
    I always tell people to avoid trying to get too much out of an Inspector or its CurrentItem before the Activate() event fires for the Inspector. The reason is that in NewInspector() and other places before Activate() fires your Inspector object reference is a "weak" object reference, where not every property is fully filled in. In and after Activate() the reference is a "strong" object reference and all properties are available.
     
    The PM who owns the Outlook object model on the Outlook team once told me that before the object is a strong reference you should only access Inspector.CurrentItem and its MessageClass or Class properties, only what's needed to identify the type of item in the Inspector. Anything else is not guaranteed to be a valid property.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "se7enbit" <=?utf-8?B?c2U3ZW5iaXQ=?=> wrote in message news:180508a4-c534-4a46-9d6d-fc4c48d8c090...
    Thanks for your patience. 

    Your code runs without the error.  My mailItem is the current mail, then i set its content with htmOr. I think because it not finished loading yet ( because if error only when i have a large content of htmlOr) when i calculated the number of page. But when i try to make new mail Item ( objMailItem ), it runs fine but looks like stupid because i should close its Inspector after i finish my job. Any solutions for that?

    Word.Document doc = mailItem.GetInspector.WordEditor;            
                int numPage1 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                mailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                objMailItem.HTMLBody = htmlOr;
                objMailItem.GetInspector.Activate();
                doc = objMailItem.GetInspector.WordEditor;           
                int numPage2 = doc.Sections[1].Range.ComputeStatistics(Word.WdStatistic.wdStatisticPages);
                objMailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard);
                mailItem.GetInspector.Activate();


    Ken Slovak MVP - Outlook
    • Marked as answer by se7enbit Friday, July 20, 2012 4:21 PM
    Thursday, July 19, 2012 1:28 PM
  • Thanks, man :).
    Friday, July 20, 2012 4:22 PM