QTP Certification Help

QTP Scripts for Excel

QTP - Working with Excel

  • Opening workbook.
  • Opening worksheets.
  • Reading data from Excel sheet.
  • Writing data to Excel sheet.
  • Saving data to Excel sheet.

Opening workbook from QTP
Set xl = CreateObject("Excel.Application")
‘to create object for the excel object
xl.workbooks.open "c:\qatutorial_template.xls“
‘to open file

Opening worksheets from QTP
Set xl = CreateObject("Excel.Application")
xl.workbooks.open "c:\qatutorial_template.xls“
xl.sheets("sheet1").activate

Reading data from Excel from QTP
Set xl = CreateObject("Excel.Application")
xl.workbooks.open "c:\qatutorial_template.xls“
xl.sheets("sheet1").activate
msgbox xl.sheets("sheet1").cells(2,2)
xl.quit

Writing data to Excel from QTP
Set xl = CreateObject("Excel.Application")
xl.workbooks.open "c:\qatutorial_template.xls“
xl.sheets("sheet1").activate
xl.sheets("sheet1").cells(2,2) = "mercury123"

Saving excel file from QTP
Set xl = CreateObject("Excel.Application")
xl.workbooks.open "c:\qatutorial_template.xls“
xl.sheets("sheet1").activate
xl.sheets("sheet1").cells(2,2) = "mercury123“
xl.Save
xl.quit