# EXCEL Ruby not working

**URL:** <https://www.ruby-forum.com/t/excel-ruby-not-working/79498>\
**Category:** Ruby\
**Created:** [December 15, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498 "2006-12-15T19:01:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 15, 2006, 7:01pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/1 "2006-12-15T19:01:39Z")

</div>

I get a file name in the directory and modify the cells with making  
bold etc.  
when excel.visible=TRUE it works. However, when excel.visible=FALSE  
nothing happens.  
Why?

newdir=Dir.getwd  
x=1  
excel = WIN32OLE::new(“excel.application”)  
excel.visible=FALSE  
Dir["\*.xls"].each { |file|

book = excel.Workbooks.Open("#{newdir}\#{file}")  
sheetname=File.basename("#{file.upcase}",".XLS")  
excel.Worksheets("#{sheetname}").Name=“Sheet1”

excel.Worksheets(“Sheet1”).Range(“A:A”).NumberFormat = (“yyyy-mm-dd  
hh:mm:ss”)  
excel.Worksheets(“Sheet1”).Range(“a1:g1”).Font.Bold = 1  
excel.Worksheets(“Sheet1”).Range(“a1:g1”).Interior.ColorIndex = 6  
excel.Worksheets(“Sheet1”).Columns.AutoFit  
excel.Worksheets(“Sheet1”).Select  
excel.Worksheets(“Sheet1”).Rows(“2:2”).Select  
excel.ActiveWindow.FreezePanes =1

excel.DisplayAlerts = 0  
book.SaveAs("#{file}")  
excel.DisplayAlerts = 1  
book.close true

}  
excel.quit()

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 15, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/2 "2006-12-15T20:54:01Z")

</div>

anon1m0us wrote:

> I get a file name in the directory and modify the cells with making  
> bold etc.  
> when excel.visible=TRUE it works. However, when excel.visible=FALSE  
> nothing happens.  
> Why?  
> …

Tried your code and it seems that ‘Save as…’ is putting the saved  
file into ‘My Documents’ rather than overwriting the original (which is  
in the same dir as where I saved your code into a file).

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 15, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/3 "2006-12-15T21:14:06Z")

</div>

On 12/15/06, ChrisH [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> file into ‘My Documents’ rather than overwriting the original (which is  
> in the same dir as where I saved your code into a file).

I have run into trouble in the past using relative pathnames for MS  
Office files (open, saving). Try using an absolute pathname when you  
(a) open the file and (b) save the file. That should put it in the  
correct location.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 16, 2006, 10:07am UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/4 "2006-12-16T10:07:26Z")

</div>

Hi,

anon1m0us wrote:

> }  
> excel.quit()

This will look more like Ruby is you don’t type the parentheses, and  
it’s less typing.

Speaking of Excel, I mostly use spreadsheet and parseexcel, which are  
both available here:

[http://rubyforge.org/frs/?group\_id=678&release\_id=6674](http://rubyforge.org/frs/?group_id=678&release_id=6674)

And BTW, you might consider putting your code in a begin … rescue …  
ensure … end block and, well, ensure that excel it actually quit.  
Otherwise you might (will) end up with ‘dangling’ excel instances if (or  
when) the code raises an exception.  
If/when that happens, you have to quit the dangling Excel app. from the  
task manager before you can run your program again (well without  
problems, that is).  
This happened every now and then on a Windows (2K) machine until I made  
sure that excel.quit is actually executed in case of an error.

Anyway, I think the above mentioned modules are easier to use and  
understand than using OLE automation. And the code also look a lot more  
like Ruby.

Happy rubying

Stephan

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 15, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/5 "2006-12-15T23:54:40Z")

</div>

The 2 working ways I’ve found to open/save files look like this:

x.workbooks.open(Dir.getwd + “/file.xls”) # Note the / before the file  
name

or

x.workbooks.open(“C:\ruby\files\spreadheets\file.xls”) # Note the  
escaped 's

So you’ll probably be able to use something like that.

Thanks,

Nate

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 16, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/6 "2006-12-16T16:19:42Z")

</div>

Li Chen wrote:

> Stephan KÃ¤mper wrote:
> 
> > Speaking of Excel, I mostly use spreadsheet and parseexcel, which are  
> > both available here:
> > 
> > [http://rubyforge.org/frs/?group\_id=678&release\_id=6674](http://rubyforge.org/frs/?group_id=678&release_id=6674)
> 
> Is it possible to draw graphics with it in Excel?

I never tried to, so I’m not sure.

Stephan

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 16, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/7 "2006-12-16T15:59:53Z")

</div>

Stephan KÃ¤mper wrote:

> Speaking of Excel, I mostly use spreadsheet and parseexcel, which are  
> both available here:
> 
> [http://rubyforge.org/frs/?group\_id=678&release\_id=6674](http://rubyforge.org/frs/?group_id=678&release_id=6674)

Is it possible to draw graphics with it in Excel?

Li

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 20, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/8 "2006-12-20T19:22:50Z")

</div>

The main issue is still unresolved…why when I have the excel not  
visible NOTHING happens. Only when i make excel =true(visable) then  
everyting occurs?

---

<div class="post-metadata">

**Author:** ![anon1m0us](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@anon1m0us](https://www.ruby-forum.com/u/anon1m0us)\
**Post date:** [December 20, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/excel-ruby-not-working/79498/9 "2006-12-20T19:06:10Z")

</div>

The main issue is still unresolved…why when I have the excel not  
visible NOTHING happens. Only when i make excel =true(visable) then  
everyting occurs?
