Page 1 of 1

OpenOffice 3.3.0 macro to save file as UTF8

Posted: Tue Mar 14, 2017 3:08 pm
by maroun
hi,

i'm trying to create a macro that permit to open and save an existing file( that is not utf8) as UTF8 charset so the file can display correctly all characters specially arabic characters.

the below sub permit to convert a string to utf-8

sub write_string_to_path( path, mytext, encoding )

fileaccess = createUnoService ("com.sun.star.ucb.SimpleFileAccess")

outtextstream = createUnoService ("com.sun.star.io.TextOutputStream")
outtextstream.setEncoding( encoding )

out = fileaccess.openFileWrite( path )

outtextstream.setOutputStream( out )
outtextstream.writeString( mytext )
outtextstream.closeOutput()

End Sub


how to alter it to pass a text stream or a file instead of a predefined string, or exist another macro that permit to save a file as utf8.

Re: OpenOffice 3.3.0 macro to save file as UTF8

Posted: Wed Mar 15, 2017 3:36 am
by karolus
Hallo

It seems you are completly on the wrong track, …

Without Knowledge of the used encoding, you cannot open any (text)file.

If you know the actual Encoding, and you want to convert to 'utf8' so the easiest way is on Linux-commandline:

Code: Select all

iconv --from-code=<the_actual_encoding>  --to-code=utf8  file_path
maybe there exist something similar for windows.

Re: OpenOffice 3.3.0 macro to save file as UTF8

Posted: Wed Mar 15, 2017 8:53 am
by maroun
Hi

I know about iconv, either that the file is utf8 once I open it in soffice I need to define the charset enconding in ascii filter options to utf8 so it can open the file and display correctly the Arabic words.

I need a macro that can achieve me this instead of opening the file in soffice GUI.

Re: OpenOffice 3.3.0 macro to save file as UTF8

Posted: Wed Mar 15, 2017 9:03 pm
by Sébastien C
If I may suggest...

Code: Select all

Sub testWriting
 writeEncodedText("/u/batch/testWritingUtf8.txt", "مصرف البلاد الاسلامي", "UTF-8")
End Sub


Sub writeEncodedText(myPath As String, myText As String, myEncoding As String)
 Dim myTextFile As Object, mySf As Object, myFileStream As Object

 On Error Goto fileKO

                    mySf = createUnoService("com.sun.star.ucb.SimpleFileAccess")
              myTextFile = createUnoService("com.sun.star.io.TextOutputStream" )
            myFileStream = mySf.openFileWrite(myPath)
 myTextFile.OutputStream = myFileStream
     myTextFile.Encoding = myEncoding

 myTextFile.writeString(myText & chr(10))

 myFileStream.closeOutput : myTextFile.closeOutput

 On Error Goto 0
 Exit Sub

 fileKO:
 Resume fileKO2

 fileKO2:
 On Error Resume Next
 msgBox("File write error !", 16)
 myFileStream.closeOutput : myTextFile.closeOutput

 On Error Goto 0
End Sub