vba filesystemobject EndOfStream EOF

N

Nicholas Charles

Apologies, but new to using INPUT and FileSystemObject

when i reference a MSword (.doc) file and/or some text (.txt) files using
INPUT or File System Object, it opens the whole contents as one string ie no
line feeds to seperate each row. how can i loop through each row ?

is this an issue to do with a linefeed break at end of each row ? or can
the text be split but some meansd of a charactor count and adding a line feed
first, then

Dim objFSO As FileSystemObject
Dim t As TextStream

Dim vEachLine
Dim sTempStr

Set objFSO = New FileSystemObject
Set t = objFSO.OpenTextFile(sFile, 1, False)

' does not like next line ?? (i found this code in a colleagues workbook)
vEachLine = Split(objFSO.OpenTextFile(sFile).ReadAll, vbCrLf)

' and if i use
While Not t.AtEndOfStream
Debug.Print ts.ReadLine
Wend

each 'line' is the tewxt in the whole document ?

the INPUT method ive also tried adapting is
hFile = FreeFile
Open sFILE For Input As #hFile

i = 0
j = 2
Do While Not EOF(hFile)
' code here
Loop


'close file
Close #hFile

' but this still as all contents in one string ??

Help, thanks
 
T

Tom Ogilvy

Sub ReadStraightTextFile()
Dim LineofText As String
Dim rw as Long
rw = 0
Open "C:\FILEIO\TEXTFILE.TXT" For Input As #1
Do While Not EOF(1)
Line Input #1, LineofText
rw = rw + 1
cells(rw,1).Value = LineofText
Loop
'Close the file
Close #1
End Sub


A MSWord doc file is a binary file and you will not get any meaningful input
from reading a DOC file. The above should work with a normal windows text
file.
 
N

Nicholas Charles

Tom, many thanks this has helped immensely

I thought it was something like that, re .doc file / txt file.

Regards

Nicholas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top