PC Review


Reply
Thread Tools Rate Thread

Application error when releaseing Excel COM object

 
 
Rich Wallace
Guest
Posts: n/a
 
      12th Apr 2004
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      12th Apr 2004
Hi,

Try passing o byref instead of byval.

Ken
---------------
"Rich Wallace" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Rich Wallace
Guest
Posts: n/a
 
      12th Apr 2004
Thanks Ken,

Same result.

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> Try passing o byref instead of byval.
>
> Ken
> ---------------
> "Rich Wallace" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      12th Apr 2004
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

"Rich Wallace" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      12th Apr 2004
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 A11 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 D26 with a formula(=RAND()*100000) and apply format.

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

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

oRng.NumberFormat = "$0.00"

' AutoFit columns A.

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

----------------------

"Rich Wallace" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> Thanks Ken,
>
> Same result.
>
> "Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi,
> >
> > Try passing o byref instead of byval.
> >
> > Ken
> > ---------------
> > "Rich Wallace" <(E-Mail Removed)> wrote in
> > message news:(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Rich Wallace
Guest
Posts: n/a
 
      13th Apr 2004
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
"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> "Rich Wallace" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Rich Wallace
Guest
Posts: n/a
 
      13th Apr 2004
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

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 A11 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 D26 with a formula(=RAND()*100000) and apply format.
>
> oRng = oSheet.Range("D2", "D6")
>
> oRng.Formula = "=RAND()*100000"
>
> oRng.NumberFormat = "$0.00"
>
> ' AutoFit columns A.
>
> 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
>
> ----------------------
>
> "Rich Wallace" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > Thanks Ken,
> >
> > Same result.
> >
> > "Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > Hi,
> > >
> > > Try passing o byref instead of byval.
> > >
> > > Ken
> > > ---------------
> > > "Rich Wallace" <(E-Mail Removed)> wrote in
> > > message news:(E-Mail Removed)...
> > > > 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
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
when i reference a com object by regsvr32 it then find it in the COM tab, it works ok. but when i reference a com object by referencing the .dll file i get this error in my C# application: DR Microsoft Dot NET 1 19th Jan 2008 05:01 PM
ActiveX component can't create object: 'Excel.Application' error ONLY in IIS Steve Kershaw Microsoft ASP .NET 4 17th Jul 2007 11:02 PM
IE7 and Excel marco "file error: data may have been lost" + "571212 application defined or object defined error" Joshuas Microsoft Excel Discussion 2 27th Nov 2006 09:19 PM
Declaration of Excel.Application object causes error (front-end) =?Utf-8?B?YWxleGhhdHppc2F2YXM=?= Microsoft Access VBA Modules 2 13th Sep 2005 11:47 AM
Server Error in '/' Application. Object reference not set to an instance of an object. Hugo Lara Microsoft Dot NET Framework Forms 3 28th Jan 2004 08:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:27 PM.