Download and Save an Excel Sheet using QTP
During automation we might have came across scenarios which requires selecting group of data and downloading an Excel file and then save it on a particular location.
Automating Excel sheets requires correct steps to follow. At first you have to create an Excel object and then to save, you have to choose a dynamic location and it should not be machine dependent. So here i am presenting a quick function that serves the discussed purpose.
Step 1.
Make a vbs file i.e open notepad copy the below code and save it with a .vbs extension. [Name].[vbs].
Step 2.
Add the function in QTP resources. Browse file > Settings > Resources tab.
Step 3
Now Call the function in your code where required. Use keyword 'Call'
Automating Excel sheets requires correct steps to follow. At first you have to create an Excel object and then to save, you have to choose a dynamic location and it should not be machine dependent. So here i am presenting a quick function that serves the discussed purpose.
Step 1.
Make a vbs file i.e open notepad copy the below code and save it with a .vbs extension. [Name].[vbs].
Step 2.
Add the function in QTP resources. Browse file > Settings > Resources tab.
Step 3
Now Call the function in your code where required. Use keyword 'Call'
''''''Function initializing
Public Function ExcelExport()
''''''Check excel button existence
If (Browser("name:=.*").Page("title:=.*").WebButton("html id:=some ID").Exist(1)) then Browser("name:=.*").Page("title:=.*").WebButton("html id:=[some ID]").Click
'''''Will Open and Save Excel sheetIf the above line does not work, try using GetObject method to locate already open excel file with Eval to bypass syntax error on using Getobject. It is a bug in QTP syntax checking.
Browser("name:=.*").Dialog("text:=FileDownload").WinButton("text:=&Open").Click
wait 5
'''''Creating Excel object to save the exported file
Dim objExcel, xlBook
'''''Initializes user variable to Excel object
Set objExcel=CreateObject("Excel.Application")
'''''Initializes user variable to Excel object
Set objExcel = Eval("GetObject(, ""Excel.Application"")")
'''''Initializes user variable to Workbook object
Set xlBook=objExcel.Workbooks
Let me know your comments, reviews if you found above function useful.
'''''Make active sheet visible
Set xlBook = objExcel.ActiveSheet
'''''Saves, add random number, to avoid duplicate names
xlBook.SaveAs "Exported" & EntityName & RandomNumber(1,99) & ".xls"
'''''Exits MS Excel application
objExcel.Quit
'''''Release object allocation from memory
Set xlBook = Nothing
Set objExcel = Nothing
'''''Prints a user defined friendly message
Print "Downloaded and Saved Exported Records successfully...." & vbnewline
''''''On no excel button availability
else
Print "Missing Export to Excel button"
Reporter.ReportEvent micFail,"Missing Export to Excel button","Missing Export to Excel button"
End If
End function
the line code Save as is not working...geting "un known run time error"
ReplyDelete