How do I open multiple Excel files using Ruby script?

-1

I am working on writing a ruby script to iterate through a file containing a list of file paths to open in Microsoft Excel. I read the file like this:

file_names = IO.readlines('D:\TEST_1\file_names.txt')

Next, I create an array of file names from each line of the parsed file (thus containing an array of file paths). Finally, I loop through that array with the following code, to open the documents:

require 'win32ole'
xl = WIN32OLE.new('Excel.Application')
xl.Visible = 1
file_names.each do |file_name|
wb1=xl.Workbooks.Open(file_name)
ws1=wb1.worksheets(1)
end

That first call to parse file_names.txt produces this exception, which I am having difficulty understanding:

Test4.rb:6:in 'method_missing' OLE error code:800A03EC in Microsoft Office Excel 'D:\Test_1\1.xlsx' couldnot be found. Check the spelling of the file name, and verify that the file location is correct.

if you are trying to open the file from your list most recently used files, make sure that the file has not been renamed, moved or deleted HR Error code : 0x80020009 Exception occurred. from Test4.rb:6:in 'block in ' from Test4.rb:5:in 'each' from Test4.rb:5:in ''

This error does not appear when I pass a single file name (instead of a file path) as my parameter - so why do I get it here? Any help would be much appreciated.

ruby
excel
parsing
asked on Stack Overflow Feb 27, 2013 by user2114237 • edited Feb 28, 2013 by Phil

1 Answer

1

At first look you are not using the variable "file_name" but a symbol :file_name.

file_array.each do |file_name|
  wb1=xl.Workbooks.Open(file_name)
  ws1=wb1.worksheets(1)
end
answered on Stack Overflow Feb 27, 2013 by Andreyy

User contributions licensed under CC BY-SA 3.0