Avatar of New_Alex
New_Alex
Flag for Cyprus asked on

VBA: Make an .xml UTF-8 (2 byte) file

Function SaveTextToFile(FileFullPath As String, _
 sText As String, Optional Overwrite As Boolean = False) As _
 Boolean
    
On Error GoTo ErrorHandler
Dim iFileNumber As Integer
iFileNumber = FreeFile

If Overwrite Then
    Open FileFullPath For Output As #iFileNumber
Else
    Open FileFullPath For Append As #iFileNumber
End If

Print #iFileNumber, sText
SaveTextToFile = True

ErrorHandler:
Close #iFileNumber
End Function

Open in new window


The above function saves the String contained in the variable  sText in the File given in the variable FileFullPath.

My problem is that my String contains also characters in other languages and I want to save it in a file with the extension .xml so it opens in Excel.

In the attached .zip file there are 2 files. "book1.xml" and "book2.xml".
book1.xml can be opened in an XML editor
book2.xml has been made using the aforementioned function and can not be opened in an XML editor since there is an encoding error
 
book1.xml is correct UTF-8
book2.xml made using the aforementioned function is defnitely saved as a one byte encoded file, and hex C5 et al. are illegal UTF-8, I am not sure it is ANSI, but it is a single byte encoding

Is it possible to make some modifications so the above mentioned function saves the multi-language string as an .xml UTF-8 (2 byte) file so it is compatible with other languages and opens with Excel or .xml editors?

Thank you.

The-xml-files.zip
Microsoft ExcelVB ScriptMicrosoft Office

Avatar of undefined
Last Comment
New_Alex

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Arno Koster

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
New_Alex

ASKER
WONDERFUL CODE BRO...

IT EVEN OVERWRITES THE FILE WHEN RUN TWICE !!!!!!!


THANK YOU !!!!!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck