467,926 Members | 2,108 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

Can't close EXCEL from VB.NET

I'm attempting to close EXCEL from within my VB.NET application.
Using the excel object library to write data to my spreadsheet is working
fine but when I try to quit application object it does not work. I know this
because I can still see the Excel application running in Task Manager.

How do I shut down EXCEL?
--
Thank You
Mar 31 '06 #1
  • viewed: 37486
Share:
8 Replies
You should remember to Save or Close any workbooks you have created:
myExcelApplication.Workbooks.Close()

and then call Quit:
myExcelApplication.Quit()

Mar 31 '06 #2
try to release each used object

Marshal.ReleaseComObject( excel );
excel = null;

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
"SteveS" <St****@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
I'm attempting to close EXCEL from within my VB.NET application.
Using the excel object library to write data to my spreadsheet is working
fine but when I try to quit application object it does not work. I know
this
because I can still see the Excel application running in Task Manager.

How do I shut down EXCEL?
--
Thank You

Mar 31 '06 #3
I used the quit method but it keeps a version of excel running in Task Manager.
This is a problem because when I go to open the spreadsheet it will not open.
Instantiating the Excel object places the process in task manager but I
can't seem to remove it from task manager.

I have tried the following:
Quit method

and

Marshal.ReleaseComObject( excel );
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

--
Thank You
"Jason Hales" wrote:
You should remember to Save or Close any workbooks you have created:
myExcelApplication.Workbooks.Close()

and then call Quit:
myExcelApplication.Quit()

Mar 31 '06 #4
I used the quit method but it keeps a version of excel running in Task Manager.
This is a problem because when I go to open the spreadsheet it will not open.
Instantiating the Excel object places the process in task manager but I
can't seem to remove it from task manager.

I have tried the following:
Quit method

and

Marshal.ReleaseComObject( excel );
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
--
Thank You
"Alexey Smirnov" wrote:
try to release each used object

Marshal.ReleaseComObject( excel );
excel = null;

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
"SteveS" <St****@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
I'm attempting to close EXCEL from within my VB.NET application.
Using the excel object library to write data to my spreadsheet is working
fine but when I try to quit application object it does not work. I know
this
because I can still see the Excel application running in Task Manager.

How do I shut down EXCEL?
--
Thank You


Mar 31 '06 #5
Alexey is correct. Every single Excel object that you created must be
released and nulled out. This includes all Worksheets, Ranges, etc... Please
see this MSDN article:
http://support.microsoft.com/default...;EN-US;q317109

I had a similar problem and found another reference that said to do the
garbage collection twice. This worked for me. Here is my C# shutdown code.

<pre>
private void ShutDownExcel() {
if (mExcelApp != null) {
mExcelApp.DisplayAlerts = true;
mExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mExcelApp);
mExcelApp = null;
}

// Clean up memory so Excel can shut down.
GC.Collect();
GC.WaitForPendingFinalizers();

// The GC needs to be called twice in order to get the
// Finalizers called - the first time in, it simply makes
// a list of what is to be finalized, the second time in,
// it actually the finalizing. Only then will the
// object do its automatic ReleaseComObject.
GC.Collect();
GC.WaitForPendingFinalizers();
}
</pre>

Good luck,

Kim Greenlee
--
digipede - Many legs make light work.
Grid computing for the real world.
http://www.digipede.net
http://krgreenlee.blogspot.net

Apr 1 '06 #6
Hello, Steve,

Some of Excel's properties and methods will use the Excel application
object by default if an explicit object isn't specified. (E.g. the
Selection property is like that.) I found (in VB6, but maybe it's still
relevant here) that this kind of problem disappears if all the Excel
application's properties and methods explicitly specify the Excel
application object.

Cheers,
Randy
SteveS wrote:
I'm attempting to close EXCEL from within my VB.NET application.
Using the excel object library to write data to my spreadsheet is working
fine but when I try to quit application object it does not work. I know this
because I can still see the Excel application running in Task Manager.

How do I shut down EXCEL?

Apr 2 '06 #7
Kim,

I've tried your resolution and other posts offering diiferent resolutions
unfortunately the EXCEL process remains present in Task Manager.
I've attached my code in hopes that it may shed some light on the issue.
As you can see the code is quite simple.....

My code is shown below:

Dim xlapp As Excel.Application
xlapp = CType(CreateObject("Excel.Application"), Excel.Application)
Dim wb As Excel.Workbook = xlapp.Workbooks.Open(fileName)
Dim xlSheet As Excel.Worksheet

Dim currRow As Integer

xlSheet = wb.Worksheets(1)
currRow = PTLRecordNumber

xlSheet.Cells(currRow, 1) = Me.LotNumber
xlSheet.Cells(currRow, 2) = Me.ScrewMachineNumber
xlSheet.Cells(currRow, 3) = Me.PartNumber
xlSheet.Cells(currRow, 4) = Me.PlatingDate
xlSheet.Cells(currRow, 5) = 0
xlSheet.Cells(currRow, 6) = Me.LoadSize
xlSheet.Cells(currRow, 7) = Me.FullLoad
xlSheet.Cells(currRow, 8) = Me.Mean
xlSheet.Cells(currRow, 9) = Me.Hi
xlSheet.Cells(currRow, 10) = Me.Low
xlSheet.Cells(currRow, 11) = Me.PLTankNumber
xlSheet.Cells(currRow, 12) = Me.BasketNumber
xlSheet.Cells(currRow, 13) = Me.CarrierNumber
xlSheet.Cells(currRow, 14) = Me.AdhesionTest
xlSheet.Cells(currRow, 15) = Me.SolderabilityTest
xlSheet.Cells(currRow, 16) = Me.OscilineNiTank
xlSheet.Cells(currRow, 17) = Me.Comments

'ss wb.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795)
wb.Save()
wb.Close()
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.InteropServices.Marshal.ReleaseComO bject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlapp)
xlapp = Nothing
wb = Nothing
xlSheet = Nothing

GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
--
Thank You
"Kim Greenlee" wrote:
Alexey is correct. Every single Excel object that you created must be
released and nulled out. This includes all Worksheets, Ranges, etc... Please
see this MSDN article:
http://support.microsoft.com/default...;EN-US;q317109

I had a similar problem and found another reference that said to do the
garbage collection twice. This worked for me. Here is my C# shutdown code.

<pre>
private void ShutDownExcel() {
if (mExcelApp != null) {
mExcelApp.DisplayAlerts = true;
mExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(mExcelApp);
mExcelApp = null;
}

// Clean up memory so Excel can shut down.
GC.Collect();
GC.WaitForPendingFinalizers();

// The GC needs to be called twice in order to get the
// Finalizers called - the first time in, it simply makes
// a list of what is to be finalized, the second time in,
// it actually the finalizing. Only then will the
// object do its automatic ReleaseComObject.
GC.Collect();
GC.WaitForPendingFinalizers();
}
</pre>

Good luck,

Kim Greenlee
--
digipede - Many legs make light work.
Grid computing for the real world.
http://www.digipede.net
http://krgreenlee.blogspot.net

Apr 3 '06 #8
Hello, Steve,

I copied the code that you posted into the click event of a button on an
otherwise empty form. I had to make a couple of obvious changes to
account for missing pieces, but otherwise I found that your code worked
fine for me. Excel disappeared from TM on the first round of GC. (For
reference, I have copied the code I was using below. I am using VB.net
2003 on W2K SP4.)

Perhaps there is something somewhere else that is keeping Excel open on
your system.

Cheers,
Randy
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim xlapp As Excel.Application
xlapp = CType(CreateObject("Excel.Application"), _
Excel.Application)
'''Dim wb As Excel.Workbook = xlapp.Workbooks.Open(fileName)
Dim wb As Excel.Workbook = _
xlapp.Workbooks.Open("J:\Test\ExcelTest\Test.xls")
Dim xlSheet As Excel.Worksheet

Dim currRow As Integer

xlSheet = wb.Worksheets(1)
'''currRow = PTLRecordNumber
currRow = 1

'''xlSheet.Cells(currRow, 1) = Me.LotNumber
'''xlSheet.Cells(currRow, 2) = Me.ScrewMachineNumber
'''xlSheet.Cells(currRow, 3) = Me.PartNumber
'''xlSheet.Cells(currRow, 4) = Me.PlatingDate
'''xlSheet.Cells(currRow, 5) = 0
'''xlSheet.Cells(currRow, 6) = Me.LoadSize
'''xlSheet.Cells(currRow, 7) = Me.FullLoad
'''xlSheet.Cells(currRow, 8) = Me.Mean
'''xlSheet.Cells(currRow, 9) = Me.Hi
'''xlSheet.Cells(currRow, 10) = Me.Low
'''xlSheet.Cells(currRow, 11) = Me.PLTankNumber
'''xlSheet.Cells(currRow, 12) = Me.BasketNumber
'''xlSheet.Cells(currRow, 13) = Me.CarrierNumber
'''xlSheet.Cells(currRow, 14) = Me.AdhesionTest
'''xlSheet.Cells(currRow, 15) = Me.SolderabilityTest
'''xlSheet.Cells(currRow, 16) = Me.OscilineNiTank
'''xlSheet.Cells(currRow, 17) = Me.Comments
xlSheet.Cells(currRow, 1) = 1.23
xlSheet.Cells(currRow, 2) = "AB.CD"
xlSheet.Cells(currRow, 3) = Now

'ss wb.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795)
wb.Save()
wb.Close()
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.InteropServices.Marshal.ReleaseComO bject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComO bject(xlapp)
xlapp = Nothing
''' wb = Nothing
''' xlSheet = Nothing

GC.Collect()
GC.WaitForPendingFinalizers()
'''GC.Collect()
'''GC.WaitForPendingFinalizers()
'''GC.Collect()
End Sub
Apr 4 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Berman | last post: by
reply views Thread by Michael Tkachev | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.