Sunday, August 12, 2007

How to Write data in Xls Sheet through QTP

We can create Xls object (As that is created in VB) and call that objest as a function.By this we can easily write data in any Xls file placed at any where in our machine (May be on network also).
For this script there is NO need of Object Repository of QTP.
Here is the Function :


Function WriteValueinXls(filepath,sheetname,row,col,texttowrite)

Dim cellvalue1
Dim ExcelApp 'As Excel.Application
Dim NewWorkbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet

Set ExcelApp = CreateObject("Excel.Application") 'Create a new excel Object
ExcelApp.Visible = False
Set NewWorkbook = ExcelApp.Workbooks.Open(filepath)
Set worksheet= NewWorkbook.Sheets(sheetname)

worksheet.Cells(row,col)=texttowrite

NewWorkbook.Save

NewWorkbook.Close
ExcelApp.Quit
Set ExcelApp = Nothing

End Function



Call to Function :

WriteValueinXls "C:\Test\test1.xls","sheet3",2,4,"Sample Text by QTP"


Result of this Call :

This call write the data "Sample Text by QTP" in Second Row and D Column (D means 4th column of Xls).The path of Xls file include the NAME of file also.And the sheet name is also specified.Remember that Xls sheet should not be READ Only.

No comments: