mliebelt mliebelt - 1 year ago 113
Ruby Question

Ruby program with win32ole does not work any more under Windows7 64bit

I have an old ruby program that extracts values from an excel file and stores the summary in another excel file. For that purpose, the program uses the library win32ole from Ruby. After switching to a new computer with Windows 7 64bit (instead of Windows XP 32bit), Office 2007 instead of Office 2003, the program now throws an error when storing the resulting excel file:

ana.rb:120:in `method_missing': SaveAs (WIN32OLERuntimeError)
OLE error code:800A03EC in Microsoft Office Excel
'c:/my/dir' could not be accessed. The file could be corrupt, is on a server that does not react, or the file is write protected.
(German: Auf 'c:/my/dir' konnte nicht zugegriffen werden. Unter Umstaenden ist die Datei beschaedigt, befindet sich auf einem Server, der nicht mehr reagiert, oder die Datei ist schreibgeschuzetzt.)
HRESULT error code:0x80020009
Ausnahmefehler aufgetreten.
from ana.rb:120:in `save'
from ana.rb:54:in `generateReport'
from ana.rb:13:in `ana'
from ana.rb:191

The relevant parts of the program are:

def generateReport
... + reportfile)

with the report:

class EasyExcel
def initialize(path)
@path = path
@excel = excel ="excel.application")
@workbook = @excel.Application.Workbooks.Open(@path)
@cache =
def save(filename)

The line 120 is that
. The value of
at that moment is
. After some debugging, I have noticed that due to my bad ruby exception handling, after the stop of the ruby interpreter, there are 2 instances of excel hanging. So it seems the problem is really due to the changes in handling paths in Excel on Windows 7.

Does any one know the answers to the following questions:

  • What could be the reason for the failure: 64bit instead of 32bit, using Office 2007 instead of 2003, or both?

  • Is there a workaround or fix to use a bridge to Windows 7 64bit and applications like Word or Excel from Ruby?

  • How can I find which API is available from a windows application from Ruby?

The Ruby interpreter I have tried are:

  • ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]

  • ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

Answer Source

Thank's to all who added ideas and comments to my question. Finally, I found a workaround.

class EasyExcel
  def save(filename)
    dos_file = filename.gsub(/\//, "\\\\")

This replaces in the (ruby) path every forward slash with 2 backward slashes, which then will evaluated to 1 backward slash at the end.

So opening an excel with

@workbook = @excel.Application.Workbooks.Open(@path)

(with @path something like


) works, but


does not. Very strange!