QTP EXcel Objects
QTP EXcel Objects
Examples:
1) Create an Excel file Dim objExcel Set objExcel=CreateObject("Excel.Application") objExcel.Visible=True 'To make the Operations visible objExcel.Workbooks.Add objExcel.ActiveWorkbook.SaveAs "C:\Documents and Settings\bannu\Desktop\qtp.xls" objExcel.Quit Set objExcel=Nothing
2) Check existence of the File, If not exist then create the file Dim objExcel, objFso, FilePath FilePath="C:\Documents and Settings\bannu\Desktop\qtp.xls" Set objFso=CreateObject("Scripting.FileSystemObject") If Not objFso.FileExists(FilePath) Then Set objExcel=CreateObject("Excel.Application") objExcel.Workbooks.Add objExcel.ActiveWorkbook.SaveAs (FilePath) objExcel.Quit Set objExcel=Nothing End if Set objFso=Nothing -----------------a) Excel Application Object b) Excel Application c) Excel File / WorkBook
http://www.gcreddy.com/2010/12/qtp-script-1.html Page 1
3) 'Check existence of the File, If exist then Enter some data 'If not Exist then Create the filr and enter some data Dim objExcel, objFso, FilePath FilePath="C:\Documents and Settings\bannu\Desktop\qtp.xls" Set objFso=CreateObject("Scripting.FileSystemObject") Set objExcel=CreateObject("Excel.Application") If objFso.FileExists(FilePath) Then objExcel.Workbooks.Open(FilePath) objExcel.Worksheets("Sheet1").Cells(1,1).value="QTP Tool" objExcel.ActiveWorkbook.Save objExcel.ActiveWorkbook.Close Else objExcel.Workbooks.Add objExcel.Worksheets("Sheet1").Cells(1,1).Value="QTP Tool" objExcel.ActiveWorkbook.SaveAs(FilePath) End if objExcel.Quit Set objExcel=Nothing Set objFso=Nothing 4) 'Capture Link Names from Google Home page and export to an Excel file (Sheet2) Dim objExcel, FilePath, objWorkBook, objSheet,r Dim oLink, Links, i, myLink FilePath="C:\Documents and Settings\bannu\Desktop\qtp.xls" 'Creating Excel Application Object Set objExcel=CreateObject("Excel.Application") 'Creating WorkBook Object Set objWorkBook=objExcel.Workbooks.Open(FilePath) 'Creating WorkSheet Object Set objSheet=objWorkBook.Worksheets("Sheet2") r=1 objSheet.Cells(r,"A")="Link Names" Set oLink=Description.Create
http://www.gcreddy.com/2010/12/qtp-script-1.html Page 2
9) Data Driven Testing by Fetching Test Data from an Excel file Dim objExcel, objWorkBook, objWorkSheet, i Set objExcel=CreateObject("Excel.Application") Set objWorkBook=objExcel.Workbooks.Open ("C:\Documents and Settings\bannu\Desktop\VBS.xls") Set objWorkSheet=objWorkBook.Worksheets("Module") Rows_Count=objWorkSheet.Usedrange.rows.count For i= 2 to Rows_Count Step 1 SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe" Dialog("text:=Login").Activate Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set objWorkSheet.Cells(i,"A") Dialog("text:=Login").WinEdit("attached text:=Password:").Set objWorkSheet.Cells(i,"B") Wait 2 Dialog("text:=Login").WinButton("text:=OK").Click Window("Flight Reservation").Close Next objWorkBook.Close Set objWorkSheet=Nothing Set objWorkBook=Nothing objExcel.Quit Set objExcel=Nothing ---------------------------10) 'Data Driven Testing by Fetching Test Data from an Excel file and Export the Result to the Same file Dim objExcel, objWorkBook, objWorkSheet, i Set objExcel=CreateObject("Excel.Application") Set objWorkBook=objExcel.Workbooks.Open ("C:\Documents andSettings\bannu\Desktop\VBS.xls") Set objWorkSheet=objWorkBook.Worksheets("Module")
http://www.gcreddy.com/2010/12/qtp-script-1.html Page 5
--------------------------11) Data Driven Testing by Fetching Test Data from an Excel file and Export the Result and Message to the Same file Dim objExcel, objWorkBook, objWorkSheet, i, Result, Error_Message Set objExcel=CreateObject("Excel.Application") Set objWorkBook=objExcel.Workbooks.Open ("C:\Documents and Settings\bannu\Desktop\VBS.xls") Set objWorkSheet=objWorkBook.Worksheets("Module") Rows_Count=objWorkSheet.Usedrange.rows.count For i= 2 to Rows_Count Step 1
http://www.gcreddy.com/2010/12/qtp-script-1.html
Page 6
http://www.gcreddy.com/2010/12/qtp-script-1.html
Page 8