Saturday, April 3, 2010

VBScript - Read a Text File Into a Microsoft Excel Spreadsheet

Read a Text File Into a Microsoft Excel Spreadsheet

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

objExcel.Workbooks.Add()

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\file.txt", ForReading)

x = 1

Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
pos = InStr(strLine, " ")
strLeft = Left(strLine, pos - 1)
strRight = Right(strLine, len(strLine) - pos)

objExcel.Cells(x, 1) = strLeft
objExcel.Cells(x, 2) = strRight
x = x + 1
Loop

objFile.Close

[Via]