473,396 Members | 1,784 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,396 software developers and data experts.

Closing an Excel instance

I wrote a code which exports an access table to excel and then manipulates it.

it works OK, but leaves an EXCEL.EXE line in the task bar...
what am I doing wrong?
Expand|Select|Wrap|Line Numbers
  1. Sub PrepareExcel(FileNme As String)
  2.  
  3. Dim xlWb As Excel.Workbook
  4. Dim xlSh As Excel.Worksheet
  5. Dim Rng As Range
  6. Dim LastRow As Long
  7. Dim LastCol As String
  8. Dim LastCell As String
  9.  
  10.     Set xlWb = Workbooks.Open(CurrentProject.Path & "\Exports\" & FileNme & ".xls")
  11.     Set xlSh = xlWb.Worksheets(FileNme)
  12.     Set Rng = xlSh.Cells
  13.  
  14.    code
  15. .
  16. .
  17. .
  18.  
  19. Finish:
  20.     xlWb.Application.DisplayAlerts = False     
  21.     xlWb.Save
  22.     xlWb.Application.DisplayAlerts = True
  23.     xlWb.Close        ' doesn't really close anything...
  24.  
  25.     Set xlSh = Nothing
  26.     Set Rng = Nothing
  27.     Set xlWb = Nothing
  28. End Sub
  29.  
Thanks in advance,

Izhar
Aug 31 '10 #1
9 20896
after reading many pages of people with the same problem I realized that the code is relevant to the closing proccess so here's the whole thing
Expand|Select|Wrap|Line Numbers
  1. Sub PrepareExcel(FileNme As String)
  2.  
  3. Dim xlWb As Excel.Workbook
  4. Dim xlSh As Excel.Worksheet
  5. Dim Rng As Range
  6. Dim LastRow As Long
  7. Dim LastCol As String
  8. Dim LastCell As String
  9.  
  10.     Set xlWb = Workbooks.Open(CurrentProject.Path & "\Exports\" & FileNme & ".xls")
  11.     Set xlSh = xlWb.Worksheets(FileNme)
  12.     Set Rng = xlSh.Cells
  13.  
  14.     ' Find the last occupied row, column and cell using another function
  15.     LastRow = Last(1, Rng)
  16.     LastCol = Chr(Last(2, Rng) + 64)
  17.     LastCell = Last(3, Rng)
  18.     With xlSh
  19.         ' Change the format of the date column to "dd/mm/yyyy hh:mm:ss"
  20.         .Range("A2:A" & LastRow & " ").NumberFormat = "dd/mm/yyyy hh:mm:ss"
  21.  
  22.         'Sort the date column
  23.         .Range("A2:" & LastCell & "").Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo, _
  24.             OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
  25.  
  26.         'set color for the top row
  27.         .Range("A1:" & LastCol & "1").Interior.Color = RGB(172, 137, 243)
  28.         .Rows("2").Select       ' Freeze the Top Row
  29.         ActiveWindow.FreezePanes = True
  30.         .Columns("A:B").EntireColumn.AutoFit     'set column width
  31.     End With
  32.  
  33. Finish:
  34.     Set Rng = Nothing
  35.     Set xlSh = Nothing
  36.     xlWb.Application.DisplayAlerts = False     ' save as T_Average.xls
  37.     xlWb.Save
  38.     xlWb.Application.DisplayAlerts = True
  39.     xlWb.Close
  40.     Set xlWb = Nothing
  41. End Sub
  42.  
Thanks again,
Izhar
Aug 31 '10 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Is this code running from within Access or from within another instance of excel?

I dont see you creating an Excel appplication in your code, thats why I am wondering.

Have you tried:
Expand|Select|Wrap|Line Numbers
  1. xlWb.Application.Close
as it is the application you want to close, not the workbook(well maybe its both :O)
Sep 1 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
Smiley is right that when you close the current workbook you are not closing the application - it is similar to when you close all active worksheets from within Excel itself, as the very last worksheet closed also closes the current active workbook but leaves Excel running until it is exited from the menu.

You can use the Quit method of the application to close the application and exit Excel (there is no Close method of the Excel Application object, unfortunately):

Expand|Select|Wrap|Line Numbers
  1. xlWb.Application.Quit
-Stewart
Sep 1 '10 #4
TheSmileyCoder
2,322 Expert Mod 2GB
Yes, of course it should be .Quit. My bad, thank your for pointing that out, Delarna.
Sep 1 '10 #5
ADezii
8,834 Expert 8TB
As previously indicated, you need to incorporate the Excel Application Object into your Logic, then properly dispose of it, as in:
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As Excel.Application
  2. Dim xlWb As Excel.Workbook
  3. Dim xlSh As Excel.Worksheet
  4.  
  5. Set xlApp = New Excel.Application
  6.  
  7. Set xlWb = xlApp.Workbooks.Open("C:\Test\Test.xls")
  8.  
  9. '...
  10.  
  11. xlApp.DisplayAlerts = False
  12. xlWb.Save
  13. xlApp.DisplayAlerts = True
  14.  
  15. xlWb.Close
  16. xlApp.Quit
  17.  
  18. Set xlWb = Nothing
  19. Set xlApp = Nothing
P.S. - Reference Code Lines 1, 5, 7, 11, 13, 16, 19.
Sep 1 '10 #6
Hi everyone,
Thank you very much for the help,
I tried everything that you told me.
While debugging I saw that xlWb.Close closes the excel rectangle from the task bar but leaves the instance in the task manager.
xlApp.quit doesn't seem to do anything... (that's why I left it out in the first place).
Any thoughts?
Expand|Select|Wrap|Line Numbers
  1. Sub PrepareExcel(FileNme As String)
  2.  
  3.     Dim xlApp As Excel.Application
  4.     Dim xlWb As Excel.Workbook
  5.     Dim xlSh As Excel.Worksheet
  6.  
  7.     Set xlApp = New Excel.Application
  8.     Set xlWb = Workbooks.Open(CurrentProject.Path & "\Exports\" & FileNme & ".xls")
  9.     xlWb.Application.Visible = True
  10.     Set xlSh = xlWb.Worksheets(FileNme)
  11.     Set Rng = xlSh.Cells
  12.  
  13.     ...
  14.  
  15. Finish:
  16.     Set Rng = Nothing
  17.     Set xlSh = Nothing
  18.     xlApp.DisplayAlerts = False     
  19.     xlWb.Save
  20.     xlApp.DisplayAlerts = True
  21.     xlWb.Close
  22.     Set xlWb = Nothing
  23.     xlApp.Quit
  24.     Set xlApp = Nothing
  25.  
  26. End Sub
  27.  
Sep 14 '10 #7
TheSmileyCoder
2,322 Expert Mod 2GB
If you code fails, it will not properly close the excel instance. Sometimes while debuffing my code, I can end up with 5+ open (and hidden) excel applications. Are you sure the excel application is not a "left-over" from previous attempts?
Sep 14 '10 #8
What I did was open the task manager while working "step by step" through my code.
The excel instance only appears when it is opened in this function.
The code reaches the end without any breaks, so i assume all the rest is OK...
Sep 14 '10 #9
hi,

the workbook object wb.Close takes arguments. try wb.Close(false,Type.Missing,Type.Missing);

This will work.
Feb 7 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lasmit42 | last post by:
Guys I am writing a program which reads the first cell of many Excel spreadsheets and then takes some actions. The problem that I am experiencing is that each Excel instance I create persists...
8
by: Tomek | last post by:
Hi, My next question is about excel instance in VB.NET I have following code: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range oXL =...
20
by: Atchoum | last post by:
I have a DLL that opens an Excel workbook and add-in. If I close the app that calls the DLL before closing Excel, when I close Excel everything is fine. But if I close Excel while the app is still...
0
by: Wesley | last post by:
Hi, I'm creating a report in Excel using asp.net, but I can't release the Excel instance from memory. Excel.exe stays in the Windows Task Manager. Below is an example of the code I'm using: ...
3
by: Ian Dunn | last post by:
I'm simply trying to access an instance of Excel that has been opened manually by the user in order to put a few values in the existing sheet. Here's the code I've tried: Dim oXL As...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
6
by: pleaseexplaintome_2 | last post by:
Help please. The excel instance is removed from task manager when I run the code below. If I uncomment the lines pertaining to a workbook, the instance of excel is not removed from task manager. ...
5
by: =?Utf-8?B?QmFyZW4=?= | last post by:
Hi! All, I am using a ASP.NEt application, wherein i am automating Execl object. I am not able to destroy the instance, eve if I write this to destroy the instance of Excel. Please find the ...
2
by: Ronin85 | last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge garbage collector to dispose excel application object,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.