Word 2007 VSTO Document: Attempted to read or write protected memory RRS feed

  • Question

  • Hello,

    i have a VSTo document where bookmarks are getting used to insert image shapes. During that insertion in some rare cases i receive the error message:

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

    I isolated the issue to a document where i stripped any content out as far as possible. When i remove any more content of the document, it does work again. So it looks to me its related to specific document content/states.

    I have create a plain new VSTO solution for that that users the stripped document and tries to insert a given image at the bookmark position. Just open it and start it.

    Its located here.

    The code used to insert the image is:

    Bookmark bm = Controls["C5906Pic000000100000052"] as Bookmark;
    if(bm != null)
    bm.InlineShapes.AddPicture(image, ref bFalse, ref bTrue, ref missingType);


              at Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range)
              at WordDocumentPicInjection.ThisDocument.TestInjection()
              at WordDocumentPicInjection.ThisDocument.ThisDocumentStartup(Object sender, EventArgs e)
              at Microsoft.Office.Tools.Word.Document.OnStartup()
              at WordDocumentPicInjection.ThisDocument.FinishInitialization()
              at Microsoft.VisualStudio.Tools.Office.EntryPointComponentBase.Microsoft.VisualStudio.Tools.Applications.Runtime.IEntryPoint.FinishInitialization()
              at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.ExecutePhase(ExecutionPhases executionPhases)
              at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.CompleteInitialization()
              at Microsoft.VisualStudio.Tools.Office.Internal.OfficeAddInAdapterBase.ExecuteEntryPointsHelper()
              at Microsoft.VisualStudio.Tools.Office.Internal.OfficeAddInAdapterBase.ExecuteEntryPoints()
              at Microsoft.VisualStudio.Tools.Applications.AddInAdapter.Microsoft.VisualStudio.Tools.Applications.Contract.IEntryPointContract2.ExecuteEntryPoints()

    When i remove any content from the VSTO document within the attached solution, it does work. Does anyone have an idea how i can workaround that or is there any fix for that ?

    Additionally it does work most of the times. The logic is used for some years already.

    Thanks for the support,

    Wednesday, November 28, 2012 12:41 PM

All replies

  • Hi Oliver,

    Thanks for posting in the MSDN Forum.

    Bookmark bm = Controls["C5906Pic000000100000052"] as Bookmark;

    Do you sure about this index is right? It's based on my experience that is a frequently mistakes which will occur in project.

    Have a good day,


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 29, 2012 5:30 AM
  • Hi Oliver

    I question the "target" for inserting the picture. You're not passing anything to the Range parameter (the last one) of AddPicture, where the error is occurring. That means Word is going to try to insert the picture at the current selection. If the current selection does not support inserting a picture, that could cause problems. Is it your intention to be inserting the picture at the bookmark?

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 29, 2012 6:20 AM
  • Hi,

    thanks for your replies.

    @Tom: Yes the index is right. The exception would look different when it would be wrong.
    @Cindy: Yes, the intention is to insert the picture at the bookmark. It may not make sense right now because its only a stripped part of the overall application functionality. Its only to show the general issue. And the insertion does work in general that way. Only in this particular document it does not.

    Kind regards,

    Thursday, November 29, 2012 8:24 AM
  • Hi Oliver

    OK, then we'll assume that the current selection is "at" the bookmark. Next question: in what kind of structure is the bookmark? Is it just in the text? In a table? In a graphical element of some sort (please be precise)?

    Is this a *.doc or a *.docx/docm file?

    What happens if you try to insert a picture as an end-user at the same target area? Does Word do it, or is there a problem (hopefully with an error message)?

    In general terms, if only one document displays a problem, and especially if it's this kind of error message, we're usually dealing with a damaged (corrupted) document. Something in the structures has "gone bad". Since it happens when you insert a graphic, a likely candidate is the part of the document that manages graphics.

    There are trouble-shooting steps for dealing with this kind of thing, but they would all result in unlinking the VSTO solution from the document. For example, we'd try creating a new document file, then copying/pasting over all but the last paragraph mark (which often contains the structures that are damaged).

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, November 29, 2012 9:00 AM
  • Hi Cindy,

    its a docx. You find it within the test solution i've posted above. (Link)

    The bookmark in that document contain already an image before inserting a new image. Yet the error also appears when i removed the image and the bookmark only contain a blank. So in that state without the image the document only contains non-character content (blanks, tabs, breaks etc) and the error occurs.

    I was not able to reproduce the error using Word only. I've inserted the image at various positions within the bookmark.

    A damaged document would be a statement yet i'd wonder how such a state was reached as only Word/VSTO was used to edit the documents. Repairing these documents via copy/paste is not really an option in our business scenario. So i'd still hope there's another way to deal with that.

    Thanks for the feedback,

    Thursday, November 29, 2012 9:20 AM