Unknown Runtime Error while reading data from Excel

429 views
Skip to first unread message

jhansi

unread,
May 3, 2012, 5:17:35 AM5/3/12
to QTP eLearn Team
Hi team,

I am getting this runtime error, when i try to read data from my
excel
file.

Unknown runtime error

Function file: C:\QTP_Practice\xl_functions.qfl
Line (10): "xlread_cell=myxlsheet.cells(xlrow,xlcol)".

Please help me fix this problem.

Thanks
Jhansi

This is my code...

Option Explicit
Dim vURL,vRate,vMPay,vZip,vTerm,vTradeIn,vTradeInowed,vCashDown,vBrow
Dim v1,v2,v3
Dim vTDownAUT
Dim i
Dim vT1,vT2,vT3
Dim xLocation,xsheet,xRow,xCol
xLocation="C:\QTP_Practice\Ddf_data"
xsheet="Testdata"

vT1=Timer()
'Initialization of variables
vBrow="iexplore.exe"
vURL="http://www.edmunds.com/calculators/"
vRate=xlread_cell(xLocation,xsheet,xRow,4)
vMPay=xlread_cell(xLocation,xsheet,xRow,1)
vZip=xlread_cell(xLocation,xsheet,xRow,2)
vTerm =xlread_cell(xLocation,xsheet,xRow,3)
vTradeIn=xlread_cell(xLocation,xsheet,xRow,5)
vTradeInowed="1000"
vCashDown=xlread_cell(xLocation,xsheet,xRow,6)
For xRow=2 to 5
v1=cint(vTradeIn)+cint(vCashDown)
v2=v1-vTradeInowed
fstep0 vBrow,vURL
fstep1 vMpay
fstep2
fstep3
fstep4
Next
vT2=timer()
vT3=vT2-vT1
Print "The time taken" & vT3






Function fstep0(fBrow,fURL)
'Step0:Open IE and got ot URL "http://www.edmunds.com/calculators/"
Systemutil.Run fBrow , fURL
End Function

Function fstep1(fMpay)
'step1:Enter monthlypayment on page1 and click go button
Browser("IE").Page("Page1").Webedit("monthly_payment").Set
fMpay
Browser("IE").Page("Page1").WebElement("GoButton").Click
End Function

Function fstep2()
'step2:Enter remaining values and click calculate button on page2
Browser("IE").Page("Page2").WebEdit("Mpayment").Set vMpay
Browser("IE").Page("Page2").WebEdit("Rate").Set vRate
Browser("IE").Page("Page2").WebEdit("TradeIn").Set vTradeIn
Browser("IE").Page("Page2").WebEdit("Zip_Code").Set vZip
Browser("IE").Page("Page2").WebList("Term").Select vTerm
Browser("IE").Page("Page2").WebEdit("TradeInowed").Set
vTradeInowed
Browser("IE").Page("Page2").WebEdit("CashDown").Set vCashDown
Browser("IE").Page("Page2").WebElement("Calculate").Click
End Function

Function fstep3()
'Step3:Get the values from AUT and compare the values with what is
expected

vTDownAUT=Browser("IE").Page("Page2").WebElement("TDown").GetROProperty
("outertext")
xlwrite_cell xLocation,xsheet,xRow,7,vTDownAUT

'If vTDownAUT= cstr(v2) Then
If cint(vTDownAUT)= cint(v2)Then
Print "pass"
xlwrite_cell xLocation,xsheet,xRow,10,"Pass"
else
Print "fail"
Print "DownPay in the App was" & vTDownAUT
xlwrite_cell xLocation,xsheet,xRow,10,"Fail"
End If
End Function

Function fstep4()
'step4:close the browser
Browser("IE").Close
End Function




Function xlread_cell(xlpath,xlsheet,xlrow,xlcol)
' Read the value from a cell with in an excel file
'i/p:xlpath,xlsheet,xlrow,xlcol
'o/p:cell value
Dim myxlapp,myxlsheet
Set myxlapp=createobject("Excel.Application")
'open xlapp in this new created object
myxlapp.workbooks.open xlpath
Set myxlsheet=myxlapp.activeworkbook.worksheets(xlsheet)
xlread_cell=myxlsheet.cells(xlrow,xlcol) (THIS IS THE LINE WHERE I GET
AN ERROR)
myxlapp.activeworkbook.close
myxlapp.application.quit
Set myxlapp=nothing
Set myxlsheet=nothing
End Function

Function xlwrite_cell(xlpath,xlsheet,xlrow,xlcol,xldata)
Dim myxlapp,myxlsheet
Set myxlapp=createobject("Excel.Application")
myxlapp.workbooks.open xlpath
Set myxlsheet=myxlapp.activeworkbook.worksheets(xlsheet)
myxlsheet.cells(xlrow,xlcol)=xldata
myxlapp.activeworkbook.Save
myxlapp.activeworkbook.close
myxlapp.application.quit
Set myxlapp=nothing
Set myxlsheet=nothing
End Function

vijayalakshmi

unread,
May 3, 2012, 6:04:19 AM5/3/12
to qtp-ele...@googlegroups.com
Hi Jhansi,

Can you please check your excel file type(either .xls / .xlsx). If its .xls then you need to specify the extension in your code as like below.

xLocation="C:\QTP_Practice\Ddf_data,xls"  

Please let me know if its not working.

Thanks
Vijayalakshmi

vijayalakshmi

unread,
May 3, 2012, 6:16:02 AM5/3/12
to qtp-ele...@googlegroups.com
Hi Jhansi,

xRow should be initialized before the below steps.

vRate=xlread_cell(xLocation,xsheet,xRow,4) 
vMPay=xlread_cell(xLocation,xsheet,xRow,1) 

Can you please share your excel file.

Thanks
Vijayalakshmi

On Thursday, 3 May 2012 14:47:35 UTC+5:30, jhansi wrote:
On Thursday, 3 May 2012 14:47:35 UTC+5:30, jhansi wrote:
On Thursday, 3 May 2012 14:47:35 UTC+5:30, jhansi wrote:
On Thursday, 3 May 2012 14:47:35 UTC+5:30, jhansi wrote:
On Thursday, 3 May 2012 14:47:35 UTC+5:30, jhansi wrote:

Retesh Patel

unread,
May 3, 2012, 7:48:07 AM5/3/12
to vijayalakshmi, qtp-ele...@googlegroups.com
Just an FYI. You can't use xlsx. You must save an excel file with .xls and you need to use the path including file name and extension (c:\test.xls).
Reply all
Reply to author
Forward
0 new messages