Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing data from Excel to Txt File
#1
Solved: 11 Years, 4 Months ago
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
Reply
#2
Solved: 11 Years, 4 Months ago
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
Want to fast track your QTP/UFT Learning? Join our UFT Training Course
Reply
#3
Solved: 11 Years, 4 Months ago
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to export the output value of a webedit object to a new excel file. mounika6677 1 1,534 04-14-2019, 05:18 PM
Last Post: mounika6677
  Invalid file error importing xls file Makhado 0 1,424 10-04-2018, 04:33 PM
Last Post: Makhado
  Access not saved excel file randhirsinghskhn 0 1,040 09-04-2018, 07:26 AM
Last Post: randhirsinghskhn
  Want to read the data from excel and set read value in the JavaTable rajeevk7 0 2,320 07-10-2017, 04:20 PM
Last Post: rajeevk7
  dtLocalSheet empty even though excel file contains data cantorre 2 2,434 05-10-2017, 12:47 PM
Last Post: vidhi

Forum Jump:


Users browsing this thread: 1 Guest(s)