[Solved] Read plain text file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Lazy-legs
Posts: 71
Joined: Mon Oct 08, 2007 1:33 am
Location: Århus-Berlin

[Solved] Read plain text file

Post 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!
Last edited by Lazy-legs on Thu Aug 05, 2010 12:47 pm, edited 1 time in total.
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Read plain text file

Post 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.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
User avatar
Lazy-legs
Posts: 71
Joined: Mon Oct 08, 2007 1:33 am
Location: Århus-Berlin

Re: Read plain text file

Post 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!
Post Reply