Sal
Sal

Reputation: 89

open excel file on the above folder with watir/Ruby

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?

Upvotes: 0

Views: 1496

Answers (2)

przbadu
przbadu

Reputation: 6049

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"

Upvotes: 0

peter
peter

Reputation: 42207

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.

Upvotes: 1

Related Questions