Page 1 of 1

[Solved] Read plain text file

Posted: Thu Aug 05, 2010 12:25 pm
by Lazy-legs
Hello,

I muddled through creating a simple macro that reads the contents of a plain text file and inserts it in the current Writer document:

Code: Select all

Sub Dummy()
ThisDoc=ThisComponent
ThisText=ThisDoc.Text
TheCursor=ThisText.createTextCursor
DummyTxt="dummy.txt"
f1 = FreeFile()
Open DummyTxt for Input as #f1
Do while NOT EOF(f1)
 Line Input #f1, s
 TheCursor.String=s
Loop
Close #f1
End Sub
The problem is that the macro considers paragraph breaks in the text file as end of file, so it only reads the first paragraph of the file. How do I make it read the entire file? Thanks!

Re: Read plain text file

Posted: Thu Aug 05, 2010 12:33 pm
by B Marcelly
Lazy-legs wrote:The problem is that the macro considers line breaks in the text file as end of file, so it only reads the first paragraph of the file. How do I make it read the entire file?
Are you sure ? :mrgreen:
Modify your code as this :

Code: Select all

Do while NOT EOF(f1)
  Line Input #f1, s
  TheCursor.String=s
  MsgBox(s)
Loop
Run it.

Re: Read plain text file

Posted: Thu Aug 05, 2010 12:45 pm
by Lazy-legs
Ah, silly me, I simply forgot to move the cursor!

Code: Select all

Do while NOT EOF(f1)
 Line Input #f1, s
 TheCursor.String=s+Chr(13)
 TheCursor.collapseToEnd
Loop
Now it works like a charm. Thanks, Bernard!