Visit: [Link].
com for QTP Information
[Link]
Excel Scripts Examples
1) Create an Excel file, enter some data and save the file using Excel object
1) Dim objexcel
2) Set objExcel = createobject(“[Link]”)
3) [Link] = True
4) [Link]
5) [Link](1, 1).Value = “Testing”
6) [Link](“f:[Link]”)
7) [Link]
2) Data Driven Testing through an External Excel Sheet
1) Set myExcel=Createobject(“[Link]”)
2) Set myFile=[Link] (“C:Documents and SettingsadminMy
[Link]”)
3) Set mySheet=[Link](“Sheet1″)
4) Rows_Count=[Link]
5) For i= 1 to Rows_Count
6) Agent=[Link](i,”A”)
7) pwd=[Link](i,”B”)
1
Visit: [Link] for QTP Information
8) [Link] “C:Program FilesMercury InteractiveQuickTest
[Link]”,”",”C:Program FilesMercury
InteractiveQuickTest Professionalsamplesflightapp”,”open”
9) Dialog(“Login”).Activate
10) Dialog(“Login”).WinEdit(“Agent Name:”).Set Agent
11) Dialog(“Login”).WinEdit(“Password:”).SetSecure pwd
12) Dialog(“Login”).WinEdit(“Password:”).Type micReturn
13) Window(“Flight Reservation”).Close
14) Next
3) Compare two excel files
Set objExcel = CreateObject(“[Link]”)
[Link] = True
Set objWorkbook1= [Link](“E:[Link]”)
Set objWorkbook2= [Link](“E:[Link]”)
Set objWorksheet1= [Link](1)
Set objWorksheet2= [Link](1)
For Each cell In [Link]
If [Link] <> [Link]([Link]).Value Then
msgbox “value is different”
Else
msgbox “value is same”
End If
Next
[Link]
[Link]
[Link]
2
Visit: [Link] for QTP Information
set objExcel=nothing
4) Data Driven Testing using Data Table methods
[Link] “gcreddy”
[Link] “C:Documents and
[Link]”,1,3
n=[Link] (3).GetRowCount
For i= 1 to n
[Link](i)
Invokeapplication “C:Program FilesHPQuickTest
[Link]”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set datatable(“agent”,3)
Dialog(“Login”).WinEdit(“Password:”).Set datatable(“pwd”,3)
Dialog(“Login”).WinButton(“OK”).Click
Window(“Flight Reservation”).Close
Next
Example 2):
[Link] “gcreddy”
[Link] “C:Documents and
[Link]”,1,3
n=[Link] (3).GetRowCount
For i= 1 to n
[Link](i)
VbWindow(“Form1″).Activate
VbWindow(“Form1″).VbEdit(“val1″).Set datatable(“V1″,3)
VbWindow(“Form1″).VbEdit(“val2″).Set datatable(“V2″,3)
3
Visit: [Link] for QTP Information
VbWindow(“Form1″).VbButton(“ADD”).Click
eres= [Link] (“res”,3)
ares=VbWindow(“Form1″).VbEdit(“res”).GetROProperty (“text”)
If eres=ares Then
datatable(“res”,3)=pass
else
datatable(“res”,3)=fail
End If
Next
5) Open an Excel Spreadsheet
Set objExcel = CreateObject(“[Link]”)
Set objWorkbook = [Link](“C:[Link]”)
6) Read an Excel Spreadsheet
Set objExcel = CreateObject(“[Link]”)
Set objWorkbook = [Link] _
(“C:ScriptsNew_users.xls”)
intRow = 2
Do Until [Link](intRow,1).Value = “”
[Link] “CN: ” & [Link](intRow, 1).Value
[Link] “sAMAccountName: ” & [Link](intRow, 2).Value
[Link] “GivenName: ” & [Link](intRow, 3).Value
[Link] “LastName: ” & [Link](intRow, 4).Value
intRow = intRow + 1
Loop
[Link]
4
Visit: [Link] for QTP Information
7) Add Formatted Data to a Spreadsheet
Set objExcel = CreateObject(“[Link]”)
[Link] = True
[Link]
[Link](1, 1).Value = “Test value”
[Link](1, 1).[Link] = TRUE
[Link](1, 1).[Link] = 24
[Link](1, 1).[Link] = 3
8) Sort an Excel Spreadsheet on Three Different Columns
Const xlAscending = 1
Const xlDescending = 2
Const xlYes = 1
Set objExcel = CreateObject(“[Link]”)
[Link] = True
Set objExcel = CreateObject(“[Link]”)
[Link] = True
Set objWorkbook = _
[Link](“C:ScriptsSort_test.xls”)
Set objWorksheet = [Link](1)
Set objRange = [Link]
Set objRange2 = [Link](“A1″)
Set objRange3 = [Link](“B1″)
Set objRange4 = [Link](“C1″)
[Link] objRange2,xlAscending,objRange3,,xlDescending, _
objRange4,xlDescending,xlYes
5
Visit: [Link] for QTP Information
[Link]