mainrotor
asked on
I need help closing an Excel object in my ASP.Net application
Hi Experts,
In my ASP.Net 3.5 application I open and close an Excel object like in my code below:
'Open Excel object
objXL = New Microsoft.Office.Interop.E xcel.Appli cation
objWkB = objXL.Workbooks.Open(strLa stQtrPSBD1 2)
objXL.Visible = False
'close Excel object
objWkB.Close(False)
objXL.Quit()
objRange = Nothing
objWkB = Nothing
objXL = Nothing
I want to be make sure that I close my Excel object when there is an Exception error. How do I check
If my Excel object is opened, so that I may close it?
Thanks in advance,
mrotor
In my ASP.Net 3.5 application I open and close an Excel object like in my code below:
'Open Excel object
objXL = New Microsoft.Office.Interop.E
objWkB = objXL.Workbooks.Open(strLa
objXL.Visible = False
'close Excel object
objWkB.Close(False)
objXL.Quit()
objRange = Nothing
objWkB = Nothing
objXL = Nothing
I want to be make sure that I close my Excel object when there is an Exception error. How do I check
If my Excel object is opened, so that I may close it?
Thanks in advance,
mrotor
''Releases COM objects
Imports System.Runtime.InteropServices
''Releases COM objects
Private Sub <strong class="highlight">ReleaseComponent</strong>(ByVal component As Object)
Dim count As Integer = 0
Do
count = Marshal.ReleaseComObject(component)
Loop While count <> 0
End Sub
Sorry:
''Releases COM objects
Imports System.Runtime.InteropServices
''Releases COM objects
Private Sub ReleaseComponent(ByVal component As Object)
Dim count As Integer = 0
Do
count = Marshal.ReleaseComObject(component)
Loop While count <> 0
End Sub
Example:
xlapp.Quit()
'ss xlApp.UserControl = True
'ss xlApp.Visible = True
'Attempt to close excel - notorious problem with .net not closing
all instances of Excel in task manager!!
System.Runtime.InteropServ ices.Marsh al.Release ComO bject(xlSheet)
System.Runtime.InteropServ ices.Marsh al.Release ComO bject(wb)
System.Runtime.InteropServ ices.Marsh al.Release ComO bject(xlapp)
xlapp = Nothing
wb = Nothing
xlSheet = Nothing
xlapp.Quit()
'ss xlApp.UserControl = True
'ss xlApp.Visible = True
'Attempt to close excel - notorious problem with .net not closing
all instances of Excel in task manager!!
System.Runtime.InteropServ
System.Runtime.InteropServ
System.Runtime.InteropServ
xlapp = Nothing
wb = Nothing
xlSheet = Nothing
Hi, if you want to effectively close the excel.exe instance, you need to call:
Example:
GC.Collect()
after call:System.Runtime.InteropServices.Marshal.FinalReleaseComObject(...)
Example:
objXL = New Microsoft.Office.Interop.Excel.Application
objWkB = objXL.Workbooks.Open(strLastQtrPSBD12)
objXL.Visible = False
'close Excel object
objWkB.Close(False)
objXL.Quit()
'Mark instances to finallize
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objWkB)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objXL)
objRange = Nothing
objWkB = Nothing
objXL = Nothing
'This will close your Excel instance
GC.Collect()
This is a better example of how to handle your Excel instance inside a try/catch block to effectively close it if an exception is raised:
Dim strLastQtrPSBD12 As String = "SheetName"
Dim objXL As Microsoft.Office.Interop.Excel.Application = Nothing
Dim objWkB As Microsoft.Office.Interop.Excel.Workbook = Nothing
Try
objXL = New Microsoft.Office.Interop.Excel.Application
objWkB = objXL.Workbooks.Open(strLastQtrPSBD12)
objXL.Visible = False
'close Excel object
objWkB.Close(False)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objWkB)
Catch ex As Exception
'Log error???
Finally
objXL.Quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objXL)
'This will close your Excel.exe process
GC.Collect()
End Try
very good example by yv989c
@dj_alik thanks buddy, not long ago I passed through this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
rick_gwu's solution good for server side
ASKER
rick_gwu,
I followed your example, but first I check for the process id created when my Excel object was opened. Then I make sure to only close the correct process.
I followed your example, but first I check for the process id created when my Excel object was opened. Then I make sure to only close the correct process.
Good!
Glad to help.
Glad to help.
Open in new window
Open in new window