Micro Focus QTP (UFT) Forums
Importing data from Excel to Txt File - 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: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Importing data from Excel to Txt File (/Thread-Importing-data-from-Excel-to-Txt-File)



Importing data from Excel to Txt File - kriday - 07-12-2013

Hi All
I have written the following code ti import data from excel to txt file
both excel and txt files exists in C drive with names sample.txt and Book1.xls
Code:
Dim obj_FSO, my_objFile, text_FilePath
Dim Obj_Excel, Excel_Filename, Excel_SheetName, Excel_Row, Excel_Col, v

text_FilePath = "C:\sample.txt"
Set obj_FSO = createObject("Scripting.FileSystemObject")
Set my_objFile = obj_FSO.OpenTextFile(text_FilePath, 8)

Excel_FileName = "C:\Book1.xls"
Excel_SheetName = "Sheet1"
Excel_Col =1

Set Obj_Excel = CreateObject("Excel.Application")
Obj_Excel.Workbooks.Open Excel_FileName

For i= 1 to 3
     Excel_Row =1
     v= Obj_Excel.ActiveWorkBook.Workssheets(Excel_SheetName).Cells(Excel_Row, Excel_Col)
     my_objFile.WriteLine v


Next
Obj_Excel.ActiveWorkbook.save
Obj_Excel.ActiveWorkbook.Closes
Obj_Excel.Workbooks.Close
Obj_Excel.Application.Quit
Set Obj_Excel = Nothing

Wscript.Quit
my_objFile.Close

but iam getting the following error

Code:
File not found
Set my_objFile = obj_FSO.OpenTextFile(text_FilePath, 8)

I am not understanding it
Please I need help on this

Thanks


RE: Importing data from Excel to Txt File - Ankur - 07-13-2013

There are syntax errors in your code -

Use this -

Code:
Dim obj_FSO, my_objFile, text_FilePath
Dim Obj_Excel, Excel_Filename, Excel_SheetName, Excel_Row, Excel_Col, v

text_FilePath = "C:\sample.txt"
Set obj_FSO = createObject("Scripting.FileSystemObject")
Set my_objFile = obj_FSO.OpenTextFile(text_FilePath, 8)

Excel_FileName = "C:\Book1.xls"
Excel_SheetName = "Sheet1"
Excel_Col =1

Set Obj_Excel = CreateObject("Excel.Application")
Obj_Excel.Workbooks.Open Excel_FileName

For i= 1 to 3
     Excel_Row =1
     v= Obj_Excel.ActiveWorkBook.Worksheets(Excel_SheetName).Cells(Excel_Row, Excel_Col)
     my_objFile.WriteLine v
Next

Obj_Excel.ActiveWorkbook.save
Obj_Excel.ActiveWorkbook.Close
Obj_Excel.Workbooks.Close
Obj_Excel.Application.Quit
Set Obj_Excel = Nothing

'Wscript.Quit
my_objFile.Close



RE: Importing data from Excel to Txt File - kriday - 07-13-2013

Thank you