Micro Focus QTP (UFT) Forums
How to write the code for creating excel in QTP - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: How to write the code for creating excel in QTP (/Thread-How-to-write-the-code-for-creating-excel-in-QTP)



How to write the code for creating excel in QTP - vaigundamoorthy - 02-28-2010

Hi Anyone Help to me how to write the code for creating excel sheet.
I have installed in MS excel 2007.

I am trying the below code and run through QTP. But excel sheet is not created the particular path

Code:
public strFullPath
strFullPath="D:\SST 1C\Results\Accommodation\Results.xls"
Function CreateExceFile()
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(strFullPath)) Then
Set objExcelApplication = CreateObject("Excel.Application")
objExcelApplication.Workbooks.open(strFullPath)
Else
Set objExcelApplication = CreateObject("Excel.Application")
objExcelApplication.Visible = true
objExcelApplication.Workbooks.Add
objExcelApplication.ActiveWorkbook.SaveAs strFullPath
End If
Set objResultSheet = objExcelApplication.ActiveWorkbook.Worksheets(1)
objResultSheet.Name = "Results"
objExcelApplication.ActiveWorkbook.Save
objExcelApplication.Workbooks.Close        
objExcelApplication.Quit
Set objExcelApplication = Nothing
End Function



RE: How to write the code for creating excel in QTP - pavansri - 02-28-2010

u have written function correctly but you just forgot to call it in your code thats the reason its not working..
i have added call to your function CreateExceFile() .. now its working ..

happy scripting Smile

CreateExceFile()
Code:
public strFullPath
strFullPath="D:\SST 1C\Results\Accommodation\Results.xls"
CreateExceFile()
Function CreateExceFile()
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(strFullPath)) Then
Set objExcelApplication = CreateObject("Excel.Application")
objExcelApplication.Workbooks.open(strFullPath)
Else
Set objExcelApplication = CreateObject("Excel.Application")
objExcelApplication.Visible = true
objExcelApplication.Workbooks.Add
objExcelApplication.ActiveWorkbook.SaveAs strFullPath
End If
Set objResultSheet = objExcelApplication.ActiveWorkbook.Worksheets(1)
objResultSheet.Name = "Results"
objExcelApplication.ActiveWorkbook.Save
objExcelApplication.Workbooks.Close        
objExcelApplication.Quit
Set objExcelApplication = Nothing
End Function



RE: How to write the code for creating excel in QTP - vaigundamoorthy - 02-28-2010

Hi Pavan

Thanks lot . Now it is working fine.
I wil keep you post on my clarification