# Excel is not closed in runtime using excel.Quit()

**URL:** <https://www.ruby-forum.com/t/excel-is-not-closed-in-runtime-using-excel-quit/90391>\
**Category:** Development\
**Created:** [March 28, 2007, 11:32am UTC](https://www.ruby-forum.com/t/excel-is-not-closed-in-runtime-using-excel-quit/90391 "2007-03-28T11:32:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MdSRuhul\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@MdSRuhul\_A](https://www.ruby-forum.com/u/MdSRuhul_A)\
**Post date:** [March 28, 2007, 11:32am UTC](https://www.ruby-forum.com/t/excel-is-not-closed-in-runtime-using-excel-quit/90391/1 "2007-03-28T11:32:39Z")

</div>

hi guys,  
I want to read some data from each of the files from a directory  
and save it to database.In the time of read operation each excel file is  
open but not close after read from the file. After processing of all  
files the excel is closed.  
here is the code…  
code

count=100  
i=0  
while i\<count  
i=i+1  
begin  
excel = WIN32OLE::new(‘excel.Application’) # create winole Object  
workbook = excel.Workbooks.Open("#{path}") # Open the Excel file  
worksheet = workbook.Worksheets(1) #get hold of the first worksheet  
worksheet.Select # select the worksheet  
title = worksheet.Range(‘h3’)[‘Value’] #get value of title  
excel.ActiveWorkbook.Close(0) # close the workbook  
excel.Quit() # close Excel file  
rescue  
excel.Quit()  
ensure  
excel.Quit() unless excel.nil?  
end  
end

code end

For 50/100 or more files, too many number of excel processes are shown  
in the process list of Test manager.The cpu utility becomes 100% and  
memory(RAM) becomes full and computer becomes very slow, almost hung.

Please review the code where I made mistake.

please help me.

---

<div class="post-metadata">

**Author:** ![MdSRuhul\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@MdSRuhul\_A](https://www.ruby-forum.com/u/MdSRuhul_A)\
**Post date:** [March 28, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/excel-is-not-closed-in-runtime-using-excel-quit/90391/2 "2007-03-28T14:07:22Z")

</div>

Hello,  
In message “[ruby-dev:30676] excel is not closed in runtime using  
excel.Quit()”  
on 07/03/28, “Md. Ruhul A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> For 50/100 or more files, too many number of excel processes are shown  
> in the process list of Test manager.The cpu utility becomes 100% and  
> memory(RAM) becomes full and computer becomes very slow, almost hung.

Could you show me the version of Ruby which you use?  
It seems to me that it is Win32OLE memory leak bug.  
And the bug is fixed in Ruby 1.8.6.

If you use 1.8.6 or later, then try GC.start after excel.Quit()

```
 excel.ActiveWorkbook.Close(0) # close the workbook
 excel.Quit() # close Excel file
 excel = nil # <-- add
 GC.start # <-- add

```

Or, try the creation of Excel object out of while loop scope.  
You create Excel object only at once, and call excel.quit only  
at once.

excel = WIN32OLE::new(‘excel.Application’) # create excel only at  
once  
while i\<count  
…  
workbook = excel.Workbooks.Open(“#{path}”) # Open the Excel file  
…  
excel.ActiveWorkbook.Close(0) # close the workbook  
end  
excel.Quit # excel quit only at once.

Regards,  
Masaki S.

---

<div class="post-metadata">

**Author:** ![MdSRuhul\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9e8a1a/32.png) [@MdSRuhul\_A](https://www.ruby-forum.com/u/MdSRuhul_A)\
**Post date:** [March 29, 2007, 5:15am UTC](https://www.ruby-forum.com/t/excel-is-not-closed-in-runtime-using-excel-quit/90391/3 "2007-03-29T05:15:09Z")

</div>

Dear Masaki S.,  
I use ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-mswin32]. By the  
second  
way I can do that.  
Thanks for ur help.

Amin

Subject: [ruby-dev:30677] Re: excel is not closed in runtime using  
excel.Quit()
