|
-
May 11th, 2009, 02:23 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Excel process spawned from VB will not go away
I'm using VB to create Excel templates and everything appeared to be working fine until I was stepping though some code today with my Task Manager open. I noticed that I had numerous instances of Excel open that should not be. This is the code that is being called to close the process:
vb Code:
Public Sub Close()
Try
If Not oExcel Is Nothing Then
oExcel.DisplayAlerts = False
oExcel.Quit()
oExcel = Nothing
GC.Collect()
End If
Catch ex As Exception
MessageBox.Show("TemplateReader.Close: " & ex.Message.ToString)
End Try
End Sub
When stepping through this code I am hitting the exception after the execution of the GC but the message box does not appear so the the using thinks that everything is fine.
When I close my applicataion all of the Excel processes shown by the Task Manager close as well. Can anyone help provide an explanation as to why the Excel instances do not close until the application closes?
-
May 11th, 2009, 02:25 PM
#2
Re: Excel process spawned from VB will not go away
What is oExcel defined as? Is it a Process object?
-
May 11th, 2009, 02:30 PM
#3
Re: Excel process spawned from VB will not go away
You need to release the COM object:
Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oExcel = Nothing
-
May 11th, 2009, 02:41 PM
#4
Thread Starter
Hyperactive Member
Re: Excel process spawned from VB will not go away
Thanks for the quick replies.
Unfortunatley, releasing the object did not close the prosess.
Here's the way I'm defining OExcel and how the file is opened.
vb Code:
Private oExcel As Excel.Application
Public Function Open() As Boolean
Try
oExcel = New Excel.Application
oExcel.Workbooks.Open(Filename:=Me.input)
Try
oSht = CType(oExcel.Workbooks(1).Worksheets("control"), Excel.Worksheet)
Catch ex As Exception
' ignore exception and throw one that is more meaningful
If oSht Is Nothing Then
Throw New System.Exception("Invalid Template Format: No Control Sheet")
End If
End Try
' Populate some cells.
oExcel.Visible = Me.visible
Return True
Catch ex As Exception
MessageBox.Show("TemplateReader.Open: " & ex.Message.ToString)
Return False
End Try
-
May 11th, 2009, 02:54 PM
#5
Re: Excel process spawned from VB will not go away
It works for me. I have a class for Excel which implements IDispose and this is my dispose sub:
vb.net Code:
Public Sub Dispose() Implements IDisposable.Dispose _ExcelWB.Close(False) _ExcelApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp) _ExcelWB = Nothing _ExcelApp = Nothing GC.SuppressFinalize(Me) End Sub
This clears the process for me.
-
May 11th, 2009, 03:14 PM
#6
Thread Starter
Hyperactive Member
Re: Excel process spawned from VB will not go away
Looking at your code lead me in the right direction, thanks.
I was was defining oSht as an Excel worksheet and not releasing it. Here's my final code. Still hitting the exception without displaying the message box but I'm not worried about that (for today).
vb Code:
Public Sub Close()
Try
If Not oExcel Is Nothing Then
oExcel.DisplayAlerts = False
oExcel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSht)
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
oSht = Nothing
oExcel = Nothing
GC.Collect()
End If
Catch ex As Exception
MessageBox.Show("TemplateReader.Close: " & ex.Message.ToString)
End Try
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|