473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
8 38149
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cpeters5 | last post by:
Deaa group, I am using SQLServer 2000 in an XP Sp2. I would like to do the following: I have a program running on a database server that generates some data which are loaded to the database....
3
by: David Berman | last post by:
Hi, I've written an application to do a while bunch of Excel automation. I open a file, scan through all the worksheets, extract data, and then I try to close Excel. However, I can see that I'm...
0
by: Michael Tkachev | last post by:
Hi How can I open excel application on the user computer if user downloads html file that has an "xls" extention? Right now this file opens for a IE. But I need to use just Excel. Thanks --...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
7
by: SteveS | last post by:
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...
4
by: Scott | last post by:
I'm trying to get access to open, refresh and then close an excel spreadsheet for me. Below is the code I'm using to open and close Excel, I just can't get it to refresh my data linked back to my...
32
by: RobinAG | last post by:
Hi everyone, I open an excel document, do some manipulation to it, and then close it through VBA in Access and free the references to it. When it closes, however, the process doesn't end and I...
1
by: popsoftheyear | last post by:
I'm trying to automate some very simple things in excel, but have run into a roadblock (using c++). To detect excel closing, I just monitor the Excel Process using WaitForSingleObject. In order to...
1
by: cindy7 | last post by:
how can i read excel data and import into sql using cfm??? I have a CFM form for user to import excel file into database and save file in the folder at server location. I would like to read that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.