none
Error when trying to SaveAs in Word document: "Attempted to read or write protected memory. This is often an indication other memory is corrupt" RRS feed

  • Question

  • Hi everyone, even after googling this error the solution to the problem I am having is not clear to me. My 'test' application is fairly simple so I was sure that one of you would be able to 'spot' my problem fairly easily (I hope).

    My .net framework 4 application opens an existing text file (file.xsr), formats it then saves it as (renamedfile.docx). I am using VS 2010 Pro and I have Office 2007 Pro installed. I have added Microsoft.Office.Interop.Word (Version 14.0.0.0) as a reference.

    Here is my code. I would appreciate if you could please help me in my specific case..

    Imports MOI = Microsoft.Office.Interop
    Public Class Form1
    
        Private Sub btnOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnOpen.Click
            Dim wrdApp As New MOI.Word.Application
    
            wrdApp.Visible = True
            wrdApp.Activate()
    
            Dim wrdDoc As MOI.Word.Document = wrdApp.Documents.Open("c:\temp\file.xsr", False, False, False, , , True, , , MOI.Word.WdOpenFormat.wdOpenFormatEncodedText)
    
            'Format document
            wrdDoc.Select()
            wrdApp.Selection.Font.Size = 9
    
            'Save and close
            wrdDoc.SaveAs2("c:\temp\renamedfile.docx", MOI.Word.WdSaveFormat.wdFormatDocumentDefault, , , False, , , , , , , , , , , , )
            wrdDoc.Close()
            wrdApp.Quit()
        End Sub
    End Class

    Full exception:

    System.AccessViolationException was unhandled
      Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
      Source=Word_Application
      StackTrace:
           at Microsoft.Office.Interop.Word._Document.SaveAs2(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks, Object& CompatibilityMode)
           at Word_Application.Form1.btnOpen_Click(Object sender, EventArgs e) in D:\RedirectedFolders\jeremy\My Documents\Visual Studio 2010\Projects\Word_Application\Word_Application\Form1.vb:line 16
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at Word_Application.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    Tuesday, February 7, 2012 9:18 AM

Answers

  • I don't know if this is causing the issue but Office 14 is 2010 and you have Office 12, which is 2007, installed.  The SaveAs2 method is for Office 14 and not available, as I recall, on Office 12 and potential some of the references you are using might not be valid.


    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by RJeremy Friday, February 17, 2012 12:26 PM
    Tuesday, February 7, 2012 11:13 AM

All replies

  • I don't know if this is causing the issue but Office 14 is 2010 and you have Office 12, which is 2007, installed.  The SaveAs2 method is for Office 14 and not available, as I recall, on Office 12 and potential some of the references you are using might not be valid.


    Kind Regards, Rich ... http://greatcirclelearning.com

    • Marked as answer by RJeremy Friday, February 17, 2012 12:26 PM
    Tuesday, February 7, 2012 11:13 AM
  • Hi Rich, thank you. This was indeed the problem. Referencing the Office 12 dll instead of the Office 14 one solved the problem for me. 

    Thanks for your help!

    Friday, February 17, 2012 12:26 PM