open excel file on the above folder with watir/Ruby

0

At moment I am open an Excel file like this

excel = WIN32OLE::new("excel.Application")
workbook = excel.Workbooks.Open('T:\\PointOfSale\\Projects\\Automated Testing\\MasterFile.xls')
worksheet = workbook.WorkSheets(1) # Get first workbook
site = worksheet.Range('A2').Value # Get the value at cell in worksheet.

and its fine but when i move the scrip to a server i am getting an error message that the Path/file cannot be found. So i decide to user a generic way like when i open a text files

excel = WIN32OLE::new("excel.Application")
workbook = excel.Workbooks.Open('../../../../Automated Testing/MasterFile.xls')
worksheet = workbook.WorkSheets(1) # Get first workbook
site = worksheet.Range('A2').Value # Get the value at cell in worksheet.

but i get an error message:

T:/PointOfSale/Projects/Automated Testing/CSA/Branch_Test/Res Processing/CancelRes/Canc_BE.rb:23:in method_missing': (in OLE methodOpen': ) (WIN32OLERuntimeError) OLE error code:800A03EC in Microsoft Excel '../../../../Automated Testing/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, make sure that the file has not been renamed, moved, or deleted. HRESULT error code:0x80020009 Exception occurred.

Do you have any idea?

ruby
asked on Stack Overflow Mar 21, 2013 by Sal • edited Mar 21, 2013 by Sal

2 Answers

1

You need the relative position to your scriptfile itself, something like

path = "#{File.dirname(__FILE__)}/../../Automated Testing/MasterFile.xls"
workbook = excel.Workbooks.Open(path)

I can't be sure where your script file resides so you need to adapt the number of /../ references.

answered on Stack Overflow Mar 21, 2013 by peter
0

You can also try,

path = "#{Dir.pwd}/Automated Testing/MasterFile.xls"
workbook = excel.WorkBooks.Open(path)

Dir.pwd will output your current project directory absolute path.

Dir.pwd
#> "/home/rails/projects/earthE/Blog"
answered on Stack Overflow Feb 12, 2014 by przbadu

User contributions licensed under CC BY-SA 3.0