PC Review


Reply
Thread Tools Rate Thread

Can't close EXCEL from VB.NET

 
 
=?Utf-8?B?U3RldmVT?=
Guest
Posts: n/a
 
      31st Mar 2006
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
 
Reply With Quote
 
 
 
 
Jason Hales
Guest
Posts: n/a
 
      31st Mar 2006
You should remember to Save or Close any workbooks you have created:
myExcelApplication.Workbooks.Close()

and then call Quit:
myExcelApplication.Quit()

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      31st Mar 2006
try to release each used object

Marshal.ReleaseComObject( excel );
excel = null;

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


"SteveS" <(E-Mail Removed)> wrote in message
news:B6CB5064-76FE-446E-B741-(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?Utf-8?B?U3RldmVT?=
Guest
Posts: n/a
 
      31st Mar 2006
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()
>
>

 
Reply With Quote
 
=?Utf-8?B?U3RldmVT?=
Guest
Posts: n/a
 
      31st Mar 2006
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" <(E-Mail Removed)> wrote in message
> news:B6CB5064-76FE-446E-B741-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
=?Utf-8?B?S2ltIEdyZWVubGVl?=
Guest
Posts: n/a
 
      1st Apr 2006
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.ReleaseComObject(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

 
Reply With Quote
 
R. MacDonald
Guest
Posts: n/a
 
      2nd Apr 2006
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?

 
Reply With Quote
 
=?Utf-8?B?U3RldmVT?=
Guest
Posts: n/a
 
      3rd Apr 2006
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.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(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.ReleaseComObject(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
>

 
Reply With Quote
 
R. MacDonald
Guest
Posts: n/a
 
      4th Apr 2006
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.ReleaseComObject(xlSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp)
xlapp = Nothing
''' wb = Nothing
''' xlSheet = Nothing

GC.Collect()
GC.WaitForPendingFinalizers()
'''GC.Collect()
'''GC.WaitForPendingFinalizers()
'''GC.Collect()
End Sub
 
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
error in running workbook.close() func. after close excel from win Kayıhan Microsoft C# .NET 0 29th Mar 2009 12:49 AM
Close a Excel Workbook when close a userform in Outlook lars.oyangen@hamstad.no Microsoft Outlook VBA Programming 2 8th May 2008 02:54 PM
Excel shoud not close all active books when clicking close button =?Utf-8?B?dGVjaG5vbWlrZQ==?= Microsoft Excel Misc 0 10th Jun 2005 05:35 PM
excel - Windows close button (x) should only close active workboo. =?Utf-8?B?Q29mZmVlQWRpY3Q=?= Microsoft Excel Setup 3 8th Feb 2005 04:30 AM
Excel 2000 Workbook close button hides instead of close Steven Robinson Microsoft Excel Misc 3 13th Aug 2003 11:28 PM


Features
 

Advertising
 

Newsgroups
 


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