VB script to copy the contents of a text file to excel

Dim objFSO, strTextFile, strData, strLine, arrLines

CONST ForReading = 1

'name of the text file
strTextFile = "C:\Users\vinoth.srinivasan\Desktop\PTE.txt"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'Open a new excel file to copy
Set objExcel = CreateObject("Excel.Application")

'Open an existing excel file to copy
'Set objWorkbook = objExcel.Workbooks.Open("C:\Users\vinoth.srinivasan\Documents\CIBC\vinoth.xlsx")

objExcel.Visible = True
objExcel.Workbooks.Add

'set the column and row in which data has to be copied
intRow = 1
intCol = 1

'Step through the lines
For Each strLine in arrLines
'wscript.echo strLine
objExcel.Cells(intRow, intCol).Value = strLine
intRow = intRow + 1
Next

'Cleanup
Set objFSO = Nothing

objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close

No comments:

Post a Comment