|
一下是我参考论坛上某大侠写的QTP集锦中对于EXCEL操作的代码:
Dim xl
'打开excel 文件
Function OpenExcelFile(strFilePath)
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open strFilePath
End Function
'获得指定单元格数据
Function GetCellData(strSheet,rwIndex,colIndex)
GetCellData = xl.WorkSheets(strSheet).Cells(rwIndex,colIndex)
End Function
'填充单元格数据
Function PutCellData(strSheet,rwIndex,colIndex,varData)
xl.WorkSheets(strSheet).Cells(rwIndex,colIndex) = varData
msgbox varData&"aaa"
End Function
'保存并推出
Function SaveAndQuit()
xl.activeworkbook.save
xl.Quit
Set xl = nothing
End Function
我把上述代码封装成函数,后缀为.QFL
然后在resource里面将封装的函数关联进去。
当在执行脚本是,调用到xl.activeworkbook.save 这句时提示文件已存在,我选择是覆盖已存在的文件。
脚本执行完成后,我在本地查看EXCEL文件时EXCEL打不开?
请问各位大侠是什么原因。谢谢。
脚本如下:
call OpenExcelFile("D:\data.xls")
Dim loginname,psw
loginname=GetCellData(1,2,1)
psw=GetCellData(1,2,2)
msgbox loginname
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set loginname
Dialog("Login").WinEdit("Password:").Set psw
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "090909"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Los Angeles"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").DblClick 110,26
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "hfhhf"
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set "4"
Window("Flight Reservation").WinButton("Insert Order").Click
wait 10
Dim var_GetROProperty
If Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")<>"" Then
var_GetROProperty = Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
msgbox var_GetROProperty
call PutCellData(1,2,3,var_GetROProperty)
call SaveAndQuit()
End If |
|