O
Otacon
Hi, I've a Problem when i try to save an Excel Woorkbook From VB2005, the
program crash and send me the next error message
"System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt."
please Help me.
the PC where the program was write, has Windows XP SP2 and Office 2003, and
the PC with the problem has Windows 2000 SP4 and Office XP Professional.
this is the code:
Private Sub Guardar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Guardar.Click
Dim Fecha As Date = Fecha_Asignacion.Value
SaveFileDialog1.InitialDirectory = "mydocuments"
SaveFileDialog1.Filter = "Archivo de Microsoft Excel (*.xls)|*.xls"
SaveFileDialog1.FileName = "Asignaciones " & Format(Fecha, "yyMMdd")
& ".xls"
SaveFileDialog1.FilterIndex = 1
SaveFileDialog1.RestoreDirectory = True
If SaveFileDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK Then
Dim Nombre_Archivo As String = SaveFileDialog1.FileName
Dim oExcel As New Microsoft.Office.Interop.Excel.Application()
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorksheet As Microsoft.Office.Interop.Excel.Worksheet
oExcel.DisplayAlerts = False
' Creamos un nuevo libro de trabajo de Excel.
oBook = oExcel.Workbooks.Add
'añadimos una hoja de excel que es donde vamos a trabajar
oWorksheet = oBook.Worksheets.Add
oWorksheet.Name = "Asignaciones_" & Format(Fecha, "ddMMyyyy")
oWorksheet.Activate()
oWorksheet = oBook.Worksheets.Item("hoja1")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("hoja2")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("hoja3")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("Asignaciones_" &
Format(Fecha, "ddMMyyyy"))
oWorksheet.Activate()
oWorksheet.Range("A1").Select()
Me.DataGridView1.ClipboardCopyMode =
DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
Me.DataGridView1.SelectAll()
Clipboard.SetDataObject(Me.DataGridView1.GetClipboardContent())
oWorksheet.Paste()
oWorksheet.SaveAs(Nombre_Archivo)
oWorksheet = Nothing
oBook.Save()
Do While Not oBook.Saved
System.Windows.Forms.Application.DoEvents()
Loop
oBook.Saved = True
oBook.Close(False)
oExcel.Workbooks.Close()
oExcel.Quit()
oBook = Nothing
oExcel = Nothing
System.GC.Collect()
GC.WaitForPendingFinalizers()
Try
System.GC.Collect()
Catch N As Exception
Debug.Print(N.Message)
End Try
MsgBox("El Archivo se ha generado con exito",
MsgBoxStyle.Information)
Me.DataGridView1.DataSource = Nothing
End If
end sub
and this is error Code:
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename,
Object FileFormat, Object Password, Object WriteResPassword, Object
ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object
TextCodepage, Object TextVisualLayout, Object Local)
at WindowsApplication1.Reporte1.Guardar_Click(Object sender, EventArgs e)
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.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies ************** mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
GestionCPP
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase:
file:///C:/Documents%20and%20Settings/D800782/Configuraci%F3n%20local/Apps/2.0/Q6E8CYWV.W6V/E9XRW8V8.H5R/gest..tion_fc8add938e35284e_0000.0001_a2d7162722adb1a3/GestionCPP.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Deployment
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Deployment/2.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
----------------------------------------
Microsoft.Office.Interop.Excel
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase:
file:///C:/Documents%20and%20Settings/D800782/Configuraci%F3n%20local/Apps/2.0/Q6E8CYWV.W6V/E9XRW8V8.H5R/gest..tion_fc8add938e35284e_0000.0001_a2d7162722adb1a3/Microsoft.Office.Interop.Excel.DLL
program crash and send me the next error message
"System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt."
please Help me.
the PC where the program was write, has Windows XP SP2 and Office 2003, and
the PC with the problem has Windows 2000 SP4 and Office XP Professional.
this is the code:
Private Sub Guardar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Guardar.Click
Dim Fecha As Date = Fecha_Asignacion.Value
SaveFileDialog1.InitialDirectory = "mydocuments"
SaveFileDialog1.Filter = "Archivo de Microsoft Excel (*.xls)|*.xls"
SaveFileDialog1.FileName = "Asignaciones " & Format(Fecha, "yyMMdd")
& ".xls"
SaveFileDialog1.FilterIndex = 1
SaveFileDialog1.RestoreDirectory = True
If SaveFileDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK Then
Dim Nombre_Archivo As String = SaveFileDialog1.FileName
Dim oExcel As New Microsoft.Office.Interop.Excel.Application()
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oWorksheet As Microsoft.Office.Interop.Excel.Worksheet
oExcel.DisplayAlerts = False
' Creamos un nuevo libro de trabajo de Excel.
oBook = oExcel.Workbooks.Add
'añadimos una hoja de excel que es donde vamos a trabajar
oWorksheet = oBook.Worksheets.Add
oWorksheet.Name = "Asignaciones_" & Format(Fecha, "ddMMyyyy")
oWorksheet.Activate()
oWorksheet = oBook.Worksheets.Item("hoja1")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("hoja2")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("hoja3")
oWorksheet.Delete()
oWorksheet = oBook.Worksheets.Item("Asignaciones_" &
Format(Fecha, "ddMMyyyy"))
oWorksheet.Activate()
oWorksheet.Range("A1").Select()
Me.DataGridView1.ClipboardCopyMode =
DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText
Me.DataGridView1.SelectAll()
Clipboard.SetDataObject(Me.DataGridView1.GetClipboardContent())
oWorksheet.Paste()
oWorksheet.SaveAs(Nombre_Archivo)
oWorksheet = Nothing
oBook.Save()
Do While Not oBook.Saved
System.Windows.Forms.Application.DoEvents()
Loop
oBook.Saved = True
oBook.Close(False)
oExcel.Workbooks.Close()
oExcel.Quit()
oBook = Nothing
oExcel = Nothing
System.GC.Collect()
GC.WaitForPendingFinalizers()
Try
System.GC.Collect()
Catch N As Exception
Debug.Print(N.Message)
End Try
MsgBox("El Archivo se ha generado con exito",
MsgBoxStyle.Information)
Me.DataGridView1.DataSource = Nothing
End If
end sub
and this is error Code:
************** Exception Text **************
System.AccessViolationException: Attempted to read or write protected
memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.Excel._Worksheet.SaveAs(String Filename,
Object FileFormat, Object Password, Object WriteResPassword, Object
ReadOnlyRecommended, Object CreateBackup, Object AddToMru, Object
TextCodepage, Object TextVisualLayout, Object Local)
at WindowsApplication1.Reporte1.Guardar_Click(Object sender, EventArgs e)
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.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies ************** mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
GestionCPP
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase:
file:///C:/Documents%20and%20Settings/D800782/Configuraci%F3n%20local/Apps/2.0/Q6E8CYWV.W6V/E9XRW8V8.H5R/gest..tion_fc8add938e35284e_0000.0001_a2d7162722adb1a3/GestionCPP.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 8.0.0.0
Win32 Version: 8.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/Microsoft.VisualBasic/8.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Runtime.Remoting/2.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
System.Deployment
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase:
file:///C:/WINNT/assembly/GAC_MSIL/System.Deployment/2.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
----------------------------------------
Microsoft.Office.Interop.Excel
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase:
file:///C:/Documents%20and%20Settings/D800782/Configuraci%F3n%20local/Apps/2.0/Q6E8CYWV.W6V/E9XRW8V8.H5R/gest..tion_fc8add938e35284e_0000.0001_a2d7162722adb1a3/Microsoft.Office.Interop.Excel.DLL