# Can we attach documents to excel columns using Ruby?

**URL:** <https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712>\
**Category:** Ruby\
**Created:** [January 24, 2013, 8:33pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712 "2013-01-24T20:33:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 24, 2013, 8:33pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/1 "2013-01-24T20:33:55Z")

</div>

Suppose I do have some folders in a directory. Now say directory name  
“Download”.i have some folders in that directory say -  
“document77444”,“document58745”,“document12457” so on. Now I want those  
docs to be upload in to an excel column using Ruby. Excel sheet has  
columns as below:

Requestnumber Doc1 Doc2  
============= ======== ========

77444 a.pdf c.jpeg  
58745 b.csv d.docx

so on.

Is there any good gem with ruby to meet such requirement?

I am using Ruby

Thanks,  
Arup

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 24, 2013, 8:46pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/2 "2013-01-24T20:46:15Z")

</div>

On Thu, Jan 24, 2013 at 1:34 PM, Arup R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 77444 a.pdf c.jpeg  
> 58745 b.csv d.docx
> 
> so on.
> 
> Is there any good gem with ruby to meet such requirement?

roo or spreadsheet gem. The rest is just a standard file upload if this  
is  
going over a network.

Andrew McElroy

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 24, 2013, 8:50pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/3 "2013-01-24T20:50:20Z")

</div>

Andrew Mcelroy wrote in post #1093607:

> On Thu, Jan 24, 2013 at 1:34 PM, Arup R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 77444 a.pdf c.jpeg  
> > 58745 b.csv d.docx
> > 
> > so on.
> > 
> > Is there any good gem with ruby to meet such requirement?

I want to upload the files into excel column, from the “local drive”,  
not to the internet.

Thanks

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 25, 2013, 8:38am UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/4 "2013-01-25T08:38:20Z")

</div>

Damián M. González wrote in post #1093640:

> > Arup R. wrote in post #1093606:  
> > Is there any good gem with ruby to meet such requirement?
> 
> Yes, win32ole, it’s packed with Ruby. Take a look at this website:  
> [Ruby on Windows: excel](http://rubyonwindows.blogspot.com.ar/search/label/excel)
> 
> Kind regards. Damián.

OMG!! Excellent reference you have provided. `+1` to you. Thank you very  
much. I will sure give it a try,hope what I am looking for ward,surely  
would get from here.

Thanks

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 25, 2013, 1:26am UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/5 "2013-01-25T01:26:45Z")

</div>

> Arup R. wrote in post #1093606:  
> Is there any good gem with ruby to meet such requirement?

Yes, win32ole, it’s packed with Ruby. Take a look at this website:

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

Kind regards. Damián.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 25, 2013, 11:04am UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/6 "2013-01-25T11:04:55Z")

</div>

> OMG!! Excellent reference you have provided. `+1` to you. Thank you very  
> much. I will sure give it a try,hope what I am looking for ward,surely  
> would get from here.
> 
> Thanks

You can be sure that that will work. You can also add colors, format  
the entire worksheet, also you can print the worksheet without show it  
to the user, so you can use Office hided and for magic art print the  
worksheet, the user of the app will say OMG! but is Excel, but he will  
never know, lol. You will see that win32ole use the same methods than  
Visual Basic, so you can learn about how to do things programaticaly by  
recording a macro doing the steps you want then taking a loop at the  
auto-generated script, methods for Visual Basic are almost the sames for  
win32ole, Ruby. Have fun :).

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 12:24pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/7 "2013-01-28T12:24:59Z")

</div>

Damián M. González wrote in post #1093726:

> > OMG!! Excellent reference you have provided. `+1` to you. Thank you very  
> > much. I will sure give it a try,hope what I am looking for ward,surely  
> > would get from here.
> > 
> > Thanks
> 
> You can be sure that that will work. You can also add colors, format

Can any one help me for the below code, which I draft to meet my above  
requirement?

require ‘win32ole’

#Excel Application will be started from here.  
#--------------------------------------------

excel = WIN32OLE.new(‘Excel.Application’)  
excel.visible = true  
wb=excel.workbooks.open(“E:\WIPData\Ruby\Scripts\GSL\_File\_DownLoad1.xlsx”)  
wbs= wb.Worksheets(1)  
rows=2  
column=2

until wbs.cells(rows,1).value == nil do

```
Dir.entries("E:\\WIPData\\Ruby").each do |f|

  if f == wbs.cells(rows,1).value then

    column=2
    Dir.foreach(f) do |x|

      full_path=Dir.pwd.concat("/" + f)
    wbs.cells(rows,column).values =

```

wbs.OLEObjects.Add(,full\_path,False,True,f,)  
column = column + 1

```
    end

  end

end

```

end

But getting error as :

E:\WIPData\Ruby\Scripts\>test.rb  
E:/WIPData/Ruby/Scripts/test.rb:25: syntax error, unexpected ‘,’,  
expecting ‘)’  
…).values = wbs.OLEObjects.Add(,full\_path,False,True,f,)  
… ^  
E:/WIPData/Ruby/Scripts/test.rb:25: syntax error, unexpected ‘,’,  
expecting ‘=’  
…cts.Add(,full\_path,False,True,f,)  
… ^

E:\WIPData\Ruby\Scripts\>

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 1:46pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/8 "2013-01-28T13:46:37Z")

</div>

Continued (couldn’t edit, out of time)

Try passing nil to make things clearer.

Try using a hash as the argument to pass the parameters by name,  
possible example here (untested as I don’t have excel with me at the  
moment):  
[http://www.ruby-forum.com/topic/145406](http://www.ruby-forum.com/topic/145406)

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 3:09pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/9 "2013-01-28T15:09:39Z")

</div>

Joel P. wrote in post #1094051:

> Continued (couldn’t edit, out of time)

I tried below:

Dir.entries(“E:\WIPData\Ruby”).each do |f|

```
  if f == wbs.cells(rows,1).value then

    files_dir = File.expand_path("..", Dir.pwd)
    column=2
    Dir.foreach(files_dir.concat("/" + f)) do |x|

      full_path=files_dir.concat("/" + x)
    wbs.oleobjects.add({
                            'ClassType' => nil,
                'Filename' => full_path,
                'Link' => false,
                'DisplayAsIcon' => false,
                'IconIndex' => nil,
                'IconLabel' => nil,
                'IconFileName' => nil,
                'Left' => nil,
                'Top' => nil,
                'Width' => nil,
                'Height' => nil
              })
    column = column + 1

    end

     break
  end

end

```

Getting error as :

E:\WIPData\Ruby\Scripts\>test.rb  
E:/WIPData/Ruby/Scripts/test.rb:26:in `method_missing': (in OLE method`add’: )  
(WIN32OLERuntimeError)  
OLE error code:800A03EC in Microsoft Excel  
Cannot insert object.  
HRESULT error code:0x80020009  
Exception occurred.  
from E:/WIPData/Ruby/Scripts/test.rb:26:in `block (2 levels) in

' from E:/WIPData/Ruby/Scripts/test.rb:23:in `foreach' from E:/WIPData/Ruby/Scripts/test.rb:23:in `block in ' from E:/WIPData/Ruby/Scripts/test.rb:17:in `each' from E:/WIPData/Ruby/Scripts/test.rb:17:in `'

E:\WIPData\Ruby\Scripts\>

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 1:28pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/10 "2013-01-28T13:28:38Z")

</div>

wbs.OLEObjects.Add(,full\_path,False,True,f,)

Check that you have the right number of arguments.

> **[OLEObjects.Add Method (Microsoft.Office.Interop.Excel)](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.oleobjects.add?redirectedfrom=MSDN&view=excel-pia)**
>
> Adds a new OLE object to a sheet. Returns an OLEObject object.

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 5:01pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/11 "2013-01-28T17:01:15Z")

</div>

It appears that you are passing in ‘.’ and ‘…’ into your Dir.entries  
block. That would cause the error you are getting.

Try skipping those two entries at the beginning of your block or use  
Dir#glob which does not capture ‘.’ and ‘…’

---

<div class="post-metadata">

**Author:** ![my-ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@my-ruby](https://www.ruby-forum.com/u/my-ruby)\
**Post date:** [January 28, 2013, 3:33pm UTC](https://www.ruby-forum.com/t/can-we-attach-documents-to-excel-columns-using-ruby/225712/12 "2013-01-28T15:33:31Z")

</div>

The whole point of using a Hash as an argument is so that you don’t have  
to enter all the nil values. You might need to use the “Execute” method  
for that to work, I’m not sure.

Anyway, the best way to test what works is to attempt that command  
manually using IRB. Put together a test sequence with different ways of  
using OLEObjects, and then find out which ones work. Once you’ve played  
around with it a bit you’ll be able to better understand the way it  
works.
