Base macro code (in Basic) to write to a UTF-8 text file

Is there a reasonably simple way to modify the following example code so that the output text file is UTF-8 ?

DIM sFName AS STRING

sFName = “file:///E:/Test.utf8”

OPEN sFName FOR OUTPUT ACCESS WRITE AS #1

REM Ignoring newline characters, the following string is written to the file as a single character - 0xA3.

PRINT #1 “£”

REM For UTF-8, it needs to have been written to the file as two characters - 0xC2 0xA3.

CLOSE #1

1 Like

Hello,

Please see this post →
Re: OpenOffice 3.3.0 macro to save file as UTF8

1 Like

Dear @Ratslinger,

And how can we add more lines into the existing text file ?

Thanks Ratslinger. That link taught me how to successfully modify my macro.

1 Like

@lonk,

myTextFile.writeString(myText & chr(10))

does the actual writing of the line. Instead of calling the routine from another sub (with just the one line) you would make the sub all inclusive and place your own mechanism (such as a loop) to write all content to the file.

1 Like

Dear @Ratslinger,

Sorry for such a very short question, actually I’d like to open previously saved text file and add more lines into it. From this code, how can I do next ?

@lonk,

You should ask this as a new question. What you are doing here is hijacking another question and adding different conditions. This then gets buried from others to find.

You may also/instead review Open Office Macros Explained by Andrew Pitonyak. Section 8 File Routines cover much on the topic. PDF here → OOME.

This post may also be of help → Append text in a notepad/.txt file

1 Like