OpenOffice 3.3.0 macro to save file as UTF8

Creating a macro - Writing a Script - Using the API

OpenOffice 3.3.0 macro to save file as UTF8

Postby maroun » Tue Mar 14, 2017 3:08 pm

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)
maroun
 
Posts: 10
Joined: Tue Mar 14, 2017 2:55 pm

Re: OpenOffice 3.3.0 macro to save file as UTF8

Postby karolus » Wed Mar 15, 2017 3:36 am

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   Expand viewCollapse view
iconv --from-code=<the_actual_encoding>  --to-code=utf8  file_path


maybe there exist something similar for windows.
AOO4, Libreoffice - 5.1 … 5.3.2.2 on Linux Mint17
User avatar
karolus
Volunteer
 
Posts: 843
Joined: Sat Jul 02, 2011 9:47 am

Re: OpenOffice 3.3.0 macro to save file as UTF8

Postby maroun » Wed Mar 15, 2017 8:53 am

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)
maroun
 
Posts: 10
Joined: Tue Mar 14, 2017 2:55 pm

Re: OpenOffice 3.3.0 macro to save file as UTF8

Postby Sébastien C » Wed Mar 15, 2017 9:03 pm

If I may suggest...

Code: Select all   Expand viewCollapse view
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 5.x under GNU-Linux Mageia 5.0 and M$-W XP
User avatar
Sébastien C
 
Posts: 29
Joined: Mon Jan 04, 2010 5:06 pm
Location: Meymac, France


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests