具有win32ole的Ruby程序在Windows7 64位下不再工作 [英] Ruby program with win32ole does not work any more under Windows7 64bit

查看:748
本文介绍了具有win32ole的Ruby程序在Windows7 64位下不再工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的ruby程序,从excel文件中提取值,并将摘要存储在另一个excel文件中。为此,该程序使用Ruby的库win32ole。使用Windows 7 64位(而不是Windows XP 32位)切换到新的计算机后,Office 2007而不是Office 2003,当存储生成的excel文件时,程序会抛出错误:



$ _ code ana.rb:120:在`method_missing'中:SaveAs(WIN32OLERuntimeError)
OLE错误代码:Microsoft Office Excel中的800A03EC
'c:/ my / dir'无法访问。该文件可能已损坏,在不起反应的服务器上,或该文件是写保护的。
(德语: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错误代码:0x80020009
Ausnahmefehler aufgetreten。
from ana.rb:120:在`save'
from ana.rb:54:在`generateReport'
from ana.rb:13:在`ana'
from ana.rb:191

程序的相关部分是:

  def generateReport 
...
report.save(basicdir + reportfile)
...
end

与报告:

  class EasyExcel 
def initialize(path)
@path = path
@excel = excel = WIN32OLE.new(excel.application)
@workbook = @ excel.Application.Workbooks.Open(@path)
@cache = Array.new
end
def save(filename)
saveCache
@ workbook.SaveAs (filename)
end

第120行是 @workbook .SaveAs(文件名)。此时 filename 的值为 c:/projekte/itcampus/feedback-analyse/feedback_report.xls 。经过一些调试,我注意到,由于我的糟糕的ruby异常处理,在ruby解释器停止之后,有两个excel挂起的例子。所以似乎问题的确是由于在Windows 7上Excel中处理路径的变化。



有没有人知道以下问题的答案:




  • 可能是失败的原因:64位而不是32位,使用Office 2007而不是2003,还是两者?


  • 如何从Windows应用程序中找到可用的API,如何使用桥接到Windows 7 64位和Word或Excel应用程序? Ruby?



我尝试过的Ruby解释器是:




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

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


解决方案

感谢所有为我的问题添加了想法和评论的人。最后,我发现了一个解决方法。

  class EasyExcel 
....
def save(filename)
saveCache
dos_file = filename.gsub(/ \ //,\\\\)
@ workbook.SaveAs(filename)
end

这将以(红宝石)路径替换两个反斜杠的正斜杠,然后将其评估为1个后退



所以打开一个excel与

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

(带$ code > @path 像

  C:/ projekte / itcampus / feedback-analyze / feedback / Bewertungsbogen_XX1404 .xls 

)作品,但

  @ workbook.SaveAs(c:/projekte/itcampus/feedback-analyse/feedback_report.xls)

不。很奇怪!


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
  ...
  report.save(basicdir + reportfile)
  ...
end

with the report:

class EasyExcel
  def initialize(path)
    @path = path
    @excel = excel = WIN32OLE.new("excel.application")
    @workbook = @excel.Application.Workbooks.Open(@path)
    @cache = Array.new
  end
  def save(filename)
    saveCache
    @workbook.SaveAs(filename)
  end

The line 120 is that @workbook.SaveAs(filename). The value of filename at that moment is c:/projekte/itcampus/feedback-analyse/feedback_report.xls. 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]

解决方案

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

class EasyExcel
  ....
  def save(filename)
    saveCache
    dos_file = filename.gsub(/\//, "\\\\")
    @workbook.SaveAs(filename)
  end

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

C:/projekte/itcampus/feedback-analyse/feedback/Bewertungsbogen_XX1404.xls

) works, but

@workbook.SaveAs("c:/projekte/itcampus/feedback-analyse/feedback_report.xls")

does not. Very strange!

这篇关于具有win32ole的Ruby程序在Windows7 64位下不再工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆