Im want to save text string to file, for example im using China chars, or specific German chars...
if im use Open for Ountput it save this chars as "??????,????. ?????,?????!" But if im use msgbox for debug it show true chars...
text="MICV —機械化步兵作戰車輛。"
FileNo = Freefile
Open Filename For Output As #FileNo
Print #FileNo, "<HTML><BODY>"
Print #FileNo, text
Print #FileNo, "</BODY></HTML>"
Close #FileNo
How to save text as Unicode UTF-8?
Last edited by Evgeniy on Sat Jan 18, 2020 10:57 am, edited 1 time in total.
As you're on Windows there is a WideCharToMultiByte Windows Api function
A page on using it here (but not in OO which doesn't have StrPtr or VarPtr, you'd need to rewrite without those if that's even possible)
sub test
pth ="C:\tmp\tesssst.txt" 'change to whatever
st= "MICV —機械化步兵作戰車輛。"
setUnicodeFileString pth,st
getunicodefilestring pth,st
msgbox st
end sub
sub setUnicodeFileString(pth as string,st as string)
dim b() as byte,f,flen as long
f=freefile
if dir(pth,0) <>"" then
KILL PTH
end if
if left(st,1) <> chr(&HFEFF) then st = chr(&HFEFF)& st
B= ST 'convert string to byte array and save file
Open pth For binary access write As #f
put #f,,b
Close #f
end sub
sub getUnicodeFileString(pth as string,st as string)
dim b() as byte,f,flen as long
if dir(pth,0) <>"" then
f=freefile
flen = filelen(pth)
if flen<>0 then
redim b(flen -1) 'redim byte array to size of file
Open pth For binary access read As #f
Seek #f,1
get #f,,b
Close #f
st =b 'convert to string
end if
end if
end sub
Edit: added file open sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
text="MICV —機械化步兵作戰車輛。"
FileNo = Freefile
Open Filename For Output As #FileNo
Print #FileNo, "<HTML><BODY>"
Print #FileNo, text
Print #FileNo, "</BODY></HTML>"
Close #FileNo
How to save text as Unicode UTF-8?
This works only for ascii-text. (And don't use "text" as variable.)
You have to either try to understand Jeje's solution (I don't ) or use this way to write text files:
Dim p(0) as new com.sun.star.beans.PropertyValue
p(0).Name = "FilterName"
p(0).Value = "Text"
ThisComponent.storeToURL(sURL, p())
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Sub SaveFile( path As String, content As String )
oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
' delete file if it exist
If oSFA.exists( strDatnam ) Then oSFA.kill(path)
oTextoutputStream = CreateUnoService("com.sun.star.io.TextOutputStream")
oOutputStream = oSFA.openFileWrite(path)
oTextoutputStream.setOutputStream(oOutputStream)
oTextoutputStream.writeString(content)
oTextoutputStream.closeOutput()
End Sub
Sub SaveFile( path As String, content As String )
oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
' delete file if it exist
If oSFA.exists( strDatnam ) Then oSFA.kill(path)
oTextoutputStream = CreateUnoService("com.sun.star.io.TextOutputStream")
oOutputStream = oSFA.openFileWrite(path)
oTextoutputStream.setOutputStream(oOutputStream)
oTextoutputStream.writeString(content)
oTextoutputStream.closeOutput()
End Sub