Connecting Tech Pros Worldwide Help | Site Map

Can't close EXCEL from VB.NET

  #1  
Old March 31st, 2006, 03:35 PM
SteveS
Guest
 
Posts: n/a
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
  #2  
Old March 31st, 2006, 03:55 PM
Jason Hales
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


You should remember to Save or Close any workbooks you have created:
myExcelApplication.Workbooks.Close()

and then call Quit:
myExcelApplication.Quit()

  #3  
Old March 31st, 2006, 04:05 PM
Alexey Smirnov
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


try to release each used object

Marshal.ReleaseComObject( excel );
excel = null;

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();


"SteveS" <SteveS@discussions.microsoft.com> wrote in message
news:B6CB5064-76FE-446E-B741-E9D1072930BA@microsoft.com...[color=blue]
> 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[/color]


  #4  
Old March 31st, 2006, 05:05 PM
SteveS
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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:
[color=blue]
> You should remember to Save or Close any workbooks you have created:
> myExcelApplication.Workbooks.Close()
>
> and then call Quit:
> myExcelApplication.Quit()
>
>[/color]
  #5  
Old March 31st, 2006, 05:05 PM
SteveS
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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:
[color=blue]
> try to release each used object
>
> Marshal.ReleaseComObject( excel );
> excel = null;
>
> GC.Collect();
> GC.WaitForPendingFinalizers();
> GC.Collect();
>
>
> "SteveS" <SteveS@discussions.microsoft.com> wrote in message
> news:B6CB5064-76FE-446E-B741-E9D1072930BA@microsoft.com...[color=green]
> > 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[/color]
>
>
>[/color]
  #6  
Old April 1st, 2006, 12:15 AM
Kim Greenlee
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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

  #7  
Old April 2nd, 2006, 03:45 PM
R. MacDonald
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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:
[color=blue]
> 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?[/color]
  #8  
Old April 3rd, 2006, 02:25 PM
SteveS
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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:
[color=blue]
> 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
>[/color]
  #9  
Old April 4th, 2006, 08:15 AM
R. MacDonald
Guest
 
Posts: n/a

re: Can't close EXCEL from VB.NET


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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't close EXCEL from my VB.NET application SteveS answers 7 April 5th, 2006 08:35 PM
Problem Reading Excel Spreadsheet From VB.NET Michael C# answers 4 November 21st, 2005 11:13 AM
Unable to quit Excel OLE app from vb .net Mike answers 1 July 21st, 2005 12:28 PM