Page 1 of 1

[Solved] Display a file with utf-8 format with macro

Posted: Wed Mar 15, 2017 11:42 am
by maroun
hi;

i'm trying to open a file with soffice GUI. after choosing the file to display the open office prompt the window " ascii filter options " as the file contains Arabic words so choosing the UTF-8 in character set permit to display the file properly.

i'm looking to create a macro to open the file as UTF-8 I created the 2 subs below

sub test5

oDoc = ThisComponent

url= ConvertToUrl ("/u/batch/test.utf8")

dim exportdata(0) as new com.sun.star.beans.PropertyValue
exportdata(0).Name = "FilterOptions"
exportdata(0).Value = "76"
oDoc = starDesktop.loadComponentFromURL(url, "_blank", 0, exportdata())

End sub

also I created the below sub

sub test6

oDoc = ThisComponent

url= ConvertToUrl ("/u/batch/test.utf8")

dim exportdata(0) as new com.sun.star.beans.PropertyValue
exportdata(0).Name = "FilterOptions"
exportdata(0).Value = "76"
oDoc = starDesktop.loadComponentFromURL(url, "_blank", 0, exportdata())

End sub

both subs still showing the Arabic characters as encrypted despite that when opening with soffice GUI the file is presented properly.

Re: display a file with utf-8 format with macro openOffice

Posted: Wed Mar 15, 2017 1:32 pm
by Zizi64
i'm trying to open a file with soffice GUI.
Is it a pure .txt file or it has other fileformat?

both subs still showing the Arabic characters as encrypted despite that when opening with soffice GUI the file is presented properly.
Maybe it is a missing font problem.


Can you upload your example file here?

Re: display a file with utf-8 format with macro openOffice

Posted: Wed Mar 15, 2017 2:42 pm
by maroun
attached is the file.

when I open it via the soffice gui when I define character set to utf-8 in ascii filter options in window prompt it is displaying correctly.

the macro is still showing them as encrypted.

Re: display a file with utf-8 format with macro openOffice

Posted: Wed Mar 15, 2017 3:26 pm
by Zizi64
attached is the file.
There is not any attached file in your posts of this thread. The file size limit is 128 KiB in this Forum. You can share the larger files with a free file sharing service (like the Google drive)

Re: display a file with utf-8 format with macro openOffice

Posted: Wed Mar 15, 2017 3:31 pm
by maroun
the file attached below.

the file format is utf8

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 3:53 pm
by Zizi64
Open the .txt file with a pure text editor.
I Just copy-pasted the text from the Notepad into the LibreOffice 4.4.7:
UTF8.png

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 4:01 pm
by maroun
i'm using open office 3.3 on solaris sparc 5.10 when I open it via soffice it still open encrypted.

the file that I sent you is converted via iconv to utf8 . in windows I can display the charcters but in solaris via soffice no.

attached the image of the soffice

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 4:08 pm
by karolus
sorry, there is absolutly no problem with:

Code: Select all

sub test

oDoc = ThisComponent

url= ConvertToUrl ("/u/batch/test.utf8")

other_Doc = starDesktop.loadComponentFromURL( url, "_blank", 0, array() )

End sub

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 4:11 pm
by Zizi64
i'm using open office 3.3
That is a very-very old version...

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 4:48 pm
by Sébastien C
Hello everyone
If I may ... Two solutions

Code: Select all

Sub ReadEncodedText1()
 Dim props(1) As New com.sun.star.beans.PropertyValue
 Dim      url As String

 url = convertToURL("/u/batch/test.utf8")

 props(0).Name = "FilterName"    : props(0).Value = "Text (encoded)"
 props(1).Name = "FilterOptions" : props(1).Value = "UTF8,LF"

 oDoc = StarDesktop.loadComponentFromURL(url, "_blank", 0, props())
End Sub
and

Code: Select all

Sub ReadEncodedText2()
 Dim  myTextFile As Object,  sf As Object, fileStream As Object
 Dim aLineOfText As String, url As String

 url = convertToUrl ("/u/batch/test.utf8")

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

 On Error Goto fichierKO

             fileStream = sf.openFileRead(url)
             myTextFile = createUnoService("com.sun.star.io.TextInputStream")
 myTextFile.InputStream = fileStream
    myTextFile.Encoding = "UTF-8"

 Do while not myTextFile.IsEOF
  aLineOfText = myTextFile.readLine
  msgBox(aLineOfText)
 Loop

 fileStream.closeInput : myTextFile.closeInput

 On Error Goto 0
 Exit Sub

 FichierKO:
 Resume FichierKO2

 FichierKO2:
 On Error Resume Next
 msgBox("File read error !", 16)

 fileStream.closeInput : myTextFile.closeInput
 On Error Goto 0
End Sub
Enjoy !
:D

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 5:21 pm
by maroun
Hello sebastien

I tried the first sub it still scrambling the characters.

do you have any idea on how to display in macro the image attached below.

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 5:28 pm
by maroun
Hi sebastien;


i'm getting them as per your first sub as showing below in the image. any idea on how to progress more and get the Arabic words

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 5:56 pm
by Sébastien C
Sorry, but this code works well at home. And I also agree with “Zizi64” that your softwares are a bit prehistorics ;)

I have no idea to make you display your dialog box
BUT
It seems that it is your default font that handles very badly the UTF8 ...

Start by locating a font that, since OpenOffice, handles Arabic characters. Let's call it “DejaVu Serif”. Then, try to correct the right line by:

Code: Select all

props(1).Name = "FilterOptions" : props(1).Value = "UTF8,CRLF,DejaVu Serif,en-CA"
In other words, you force
  • UTF8 : general encoding
  • CRLF : end lines encoding
  • DejaVu Serif : The font
  • En-CA : The language of the file
Is it better :?:

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 6:10 pm
by maroun
thanks sebastien the first one work well but i had problem with the file. thanks a lot for your time.

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 6:15 pm
by maroun
with this sub the issue was fixed.


sub test5 ()
cFile="/u/batch/test.utf8"
rem oDoc = ThisComponent
url = ConvertToUrl (cFile)

Dim FileProperties(1) As New com.sun.star.beans.PropertyValue


FileProperties(0).Name = "FilterName"
FileProperties(0).Value ="Text (encoded)"
FileProperties(1).Name = "FilterOptions"
FileProperties(1).Value ="UTF8,LF"

oDoc = starDesktop.loadComponentFromURL(url, "_blank", 0, FileProperties())

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 6:18 pm
by Villeroy
Sébastien C wrote:Sorry, but this code works well at home. And I also agree with “Zizi64” that your softwares are a bit prehistorics ;)

I have no idea to make you display your dialog box
BUT
It seems that it is your default font that handles very badly the UTF8 ...

Start by locating a font that, since OpenOffice, handles Arabic characters. Let's call it “DejaVu Serif”. Then, try to correct the right line by:

Code: Select all

props(1).Name = "FilterOptions" : props(1).Value = "UTF8,CRLF,DejaVu Serif,en-CA"
In other words, you force
  • UTF8 : general encoding
  • CRLF : end lines encoding
  • DejaVu Serif : The font
  • En-CA : The language of the file
Is it better :?:
Sébastien,
How/where did you find the right filter options?
 Edit: OK. I found out by opening the file as file type "Text - Choose Encoding" and then inspect ThisComponent.Args() 

Re: Display a file with utf-8 format with macro OpenOffice

Posted: Wed Mar 15, 2017 8:06 pm
by Sébastien C
As you know I am French and I read easily the fr-FR...

And in FR, we have the incredible chance to have the Bible written in fr ... Even if Andrew Pitonyak is a reference, it's MUCH MORE organized, laid out, structured.

It is very easy to find this text in pirated PDF in a relatively recent version, but I will not give the link here. The price of the rights of the book is totally donated to the community and the authors do not receive any money.

I did a search G***** with (in quotes) "UTF8" and "com.sun.star.beans.PropertyValue".

It brought me back to Bernard Marcelly's bible, a page that G***** gives to read.

But if you want the list of ALL filters, then Andrew Pitonyak is better ... On page 218 of the PDF, for example.

8-)