Command Prompt error since i am using a generic path to open an excel file

0

Command Prompt its not working since i am using a generic path to open a excel file. Here is the error message:

T:\PointOfSale\Projects\Automated Testing\TASWeb\TP\TP_Branch>ruby -rubygems Tes tTP_UK.rb TestTP_UK.rb:19:in 'method_missing': (in OLE method `Open': )(WIN32OLERuntimeEr ror) OLE error code:800A03EC in Microsoft Excel './../../../MasterFile.xls' could not 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 of most recently used files, m ake sure that the file has not been renamed, moved, or deleted. HRESULT error code:0x80020009 Exception occurred. from TestTP_UK.rb:19:in `' enter code here'

Generic path code

excel = WIN32OLE::new("excel.Application")
path = "#{File.dirname(__FILE__)}/../../../MasterFile.xls"
workbook = excel.Workbooks.Open(path)
worksheet = workbook.WorkSheets(1) # Get first workbook
site = worksheet.Range('A2').Value # Get the value at cell in worksheet.
workbook.Close
excel.Quit

Any Ideas

ruby
win32ole
asked on Stack Overflow May 23, 2013 by Sal • edited May 23, 2013 by Chuck van der Linden

1 Answer

1

I believe you need to use an absolute path rather than a relative path when opening the file:

path = File.expand_path("../../../../MasterFile.xls", __FILE__)

Note that you will also need an additional '..' when using expand_path, since the first '..' is going back from the file.

answered on Stack Overflow May 23, 2013 by Justin Ko

User contributions licensed under CC BY-SA 3.0