# How to operate an existing Excel file?

**URL:** <https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582>\
**Category:** Ruby\
**Created:** [August 8, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582 "2007-08-08T20:10:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexeyomux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@alexeyomux](https://www.ruby-forum.com/u/alexeyomux)\
**Post date:** [August 8, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582/1 "2007-08-08T20:10:46Z")

</div>

\<\<Programming Ruby 2nd\>\> teaches me how to create a new Excel file and  
add  
something to it, but now I have a task that there were some existing  
Excel  
files with data, what should I do is opening the files one by one and  
read  
the data for doing something.  
I try to use the WIN32OLE .connect() function, but it didn’t work.  
Is there other better way to do so?  
thanks a lot!

---

<div class="post-metadata">

**Author:** ![alexeyomux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@alexeyomux](https://www.ruby-forum.com/u/alexeyomux)\
**Post date:** [August 8, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582/2 "2007-08-08T20:45:08Z")

</div>

alexeyomux wrote:

> \<\<Programming Ruby 2nd\>\> teaches me how to create a new Excel file and  
> add  
> something to it, but now I have a task that there were some existing  
> Excel  
> files with data, what should I do is opening the files one by one and  
> read  
> the data for doing something.  
> I try to use the WIN32OLE .connect() function, but it didn’t work.  
> Is there other better way to do so?  
> thanks a lot!

.connect establishes a connection between your ruby and an already  
running instance of Excel.  
.open opens a new instance of Excel, whether Excel is running at the  
time or not.

After you establish a session with Excel, using either of the above  
methods, then you need to open the workbook you want.

Todd

---

<div class="post-metadata">

**Author:** ![alexeyomux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@alexeyomux](https://www.ruby-forum.com/u/alexeyomux)\
**Post date:** [August 9, 2007, 2:23am UTC](https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582/3 "2007-08-09T02:23:28Z")

</div>

alexeyomux wrote:

> \<\<Programming Ruby 2nd\>\> teaches me how to create a new Excel file and  
> add  
> something to it, but now I have a task that there were some existing  
> Excel  
> files with data, what should I do is opening the files one by one and  
> read  
> the data for doing something.  
> I try to use the WIN32OLE .connect() function, but it didn’t work.  
> Is there other better way to do so?  
> thanks a lot!

excel = WIN32OLE.new(‘Excel.Application’)  
workbook = excel.Workbooks.Open(‘c:\temp\MyWorkbook.xls’)

> **[Automating Excel with Ruby: The Application Object](http://rubyonwindows.blogspot.com/2007/03/automating-excel-with-ruby-application.html)**
>
> Automating Microsoft Excel can involve hundreds of objects, each with its own properties and methods. Let's start by looking at the top-leve...

> **[Automating Excel with Ruby: The Workbook Object](http://rubyonwindows.blogspot.com/2007/03/automating-excel-with-ruby-workbook.html)**
>
> Previously, we looked at the Excel Application object. Today, we'll take it down a level and investigate the Workbooks collection and Workbo...

> **[Ruby on Windows](http://rubyonwindows.blogspot.com/search/label/excel)**
>
> On using the Ruby programming language on the Microsoft Windows platform

David

---

<div class="post-metadata">

**Author:** ![alexeyomux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@alexeyomux](https://www.ruby-forum.com/u/alexeyomux)\
**Post date:** [August 9, 2007, 2:40am UTC](https://www.ruby-forum.com/t/how-to-operate-an-existing-excel-file/111582/4 "2007-08-09T02:40:43Z")

</div>

On Aug 8, 2:08 pm, “alexeyomux” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<\<Programming Ruby 2nd\>\> teaches me how to create a new Excel file and add  
> something to it, but now I have a task that there were some existing Excel  
> files with data, what should I do is opening the files one by one and read  
> the data for doing something.  
> I try to use the WIN32OLE .connect() function, but it didn’t work.  
> Is there other better way to do so?  
> thanks a lot!

What the other posters suggested are great tips. Personally I have  
been able to leverage a lot of great time savers using Ruby’s WIN32OLE  
and Excel. For example, reading lots of SQL data sets and pumping them  
into new Excel worksheets, reading and modifying existing Excel  
spreadsheets, etc. One recent project involved taking an SQL query  
script and exporting the results to Excel. From there that Excel file  
served as the data source for a Word mail merge. Automated all to the  
point of a scheduled job that automatically runs each reporting  
period. Good stuff for sure. And a heck of a lot less code and effort  
than doing the same thing in VB, C#, etc.
