OpenOffice 3.3.0 macro to save file as UTF8

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
maroun
Posts: 10
Joined: Tue Mar 14, 2017 2:55 pm

OpenOffice 3.3.0 macro to save file as UTF8

Post 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.
open office version: 3.3.0
operating system : solaris 10 ( sparc 5.10)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: OpenOffice 3.3.0 macro to save file as UTF8

Post 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.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
maroun
Posts: 10
Joined: Tue Mar 14, 2017 2:55 pm

Re: OpenOffice 3.3.0 macro to save file as UTF8

Post 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.
open office version: 3.3.0
operating system : solaris 10 ( sparc 5.10)
User avatar
Sébastien C
Posts: 111
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France

Re: OpenOffice 3.3.0 macro to save file as UTF8

Post 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
LibreOffice v. 7.3.2.2, under GNU-Linux Mint and, in virtualization and just for tests, LibreOffice v. 7.3.2.2 an OpenOffice v. 4.1.12 under M$-W 10 :ouch: .
Post Reply