Application error when releaseing Excel COM object

R

Rich Wallace

Hi all,

I have a VB app that runs and manages individual XLS files within a single
COM object. Upon processing the final fie, I attempt to close out the EXCEL
object and release it using
System.Runtime.InteropServices.Marshal.ReleaseComObject.

I have a Try...Catch in my routine as seen below and when I receive the
error, my Catch is never called so my app sees the release as successful,
but when I look in Task Manager, the EXCEL.EXE instance is still running and
a Windows Application error pops up...see below code snippet

Private Sub ObjectRelease(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
MsgBox(Err.Description)
Finally
o = Nothing
End Try
End Sub

Error:
Microsoft Excel: EXCEL.EXE - Application Error
The instruction as "0x77ab193d" referenced memory at "0x0207da34". The
memory could not be "read".

Memory should not be an issue on the server I am running the app on. BTW, I
do clean up any/all references to any Sheets, Workbooks/Workbook objects
prior to calling the ablove code:

'Release the Excel objects
oBook.Save()
ObjectRelease(oSheet)
oBook.Close(False)
ObjectRelease(oBook)
ObjectRelease(oBooks)
oExcel.Quit()

Any ideas??

TIA
-Rich
 
J

Jay B. Harlow [MVP - Outlook]

Rich,
The following statement requires ByRef as Ken suggests.
o = Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Should be called in a loop until it reaches zero to ensure all .NET
references are released, see the help for ReleaseComObject for details.

Also be very certain you release the COM objects in the correct order (it
appears that you are). And you release all your COM objects.

Where are you seeing the following error?
Error:
Microsoft Excel: EXCEL.EXE - Application Error
The instruction as "0x77ab193d" referenced memory at "0x0207da34". The
memory could not be "read".

Hope this helps
Jay
 
K

Ken Tucker [MVP]

Hi,

Sample on how I open and release excel

Dim oXL As Excel.Application

Dim oWB As Excel.Workbook

Dim oSheet As Excel.Worksheet

Dim oRng As Excel.Range

' Start Excel and get Application object.

oXL = CreateObject("Excel.Application")

oXL.Visible = True

' Get a new workbook.

oWB = oXL.Workbooks.Add

oSheet = oWB.ActiveSheet

' Add table headers going cell by cell.

oSheet.Cells(1, 1).Value = "First Name"

oSheet.Cells(1, 2).Value = "Last Name"

oSheet.Cells(1, 3).Value = "Full Name"

oSheet.Cells(1, 4).Value = "Salary"

' Format A1:D1 as bold, vertical alignment = center.

With oSheet.Range("A1", "D1")

..Font.Bold = True

..VerticalAlignment = Excel.XlVAlign.xlVAlignCenter

End With

' Create an array to set multiple values at once.

Dim saNames(5, 2) As String

saNames(0, 0) = "John"

saNames(0, 1) = "Smith"

saNames(1, 0) = "Tom"

saNames(1, 1) = "Brown"

saNames(2, 0) = "Sue"

saNames(2, 1) = "Thomas"

saNames(3, 0) = "Jane"

saNames(3, 1) = "Jones"

saNames(4, 0) = "Adam"

saNames(4, 1) = "Johnson"

' Fill A2:B6 with an array of values (First and Last Names).

oSheet.Range("A2", "B6").Value = saNames

' Fill C2:C6 with a relative formula (=A2 & " " & B2).

oRng = oSheet.Range("C2", "C6")

oRng.Formula = "=A2 & "" "" & B2"

' Fill D2:D6 with a formula(=RAND()*100000) and apply format.

oRng = oSheet.Range("D2", "D6")

oRng.Formula = "=RAND()*100000"

oRng.NumberFormat = "$0.00"

' AutoFit columns A:D.

oRng = oSheet.Range("A1", "D1")

oRng.EntireColumn.AutoFit()

' Manipulate a variable number of columns for Quarterly Sales Data.

' Make sure Excel is visible and give the user control

' of Excel's lifetime.

oXL.Visible = True

oXL.UserControl = True

' Make sure that you release object references.

oRng = Nothing

oXL.DisplayAlerts = False

oSheet.SaveAs("C:\Test.xls",
FileFormat:=Excel.XlFileFormat.xlXMLSpreadsheet)

oSheet = Nothing

oWB = Nothing

oXL.Quit()

Marshal.ReleaseComObject(oXL)

oXL = Nothing

GC.Collect()



Ken
 
R

Rich Wallace

Hi Jay,

I receive the error on the
System.Runtime.InteropServices.Marshal.ReleaseComObject(o) call. Ther is a
message box from Windows, but my code never hits the Catch line.

Thanks,
-Rich
 
R

Rich Wallace

Thanks Ken,

I think I'm doing the same thing (more or less) in code. It's odd because
it's sporadic and when it does fail, the error is displayed in a message
box, but it's not from my application as the code never hits the Catch
portion of my Try...Catch block.

Could it be an issue with my version of Excel perhaps?

Thanks,
-Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top