I have a very complex problem that is difficult to explain properly. There is LOTS of discussion about this across the internet, but nothing definitive. Any help, or better explanation than mine, is greatly appreciated.
Essentially, I'm just trying to write an XML file using UTF-16 with VBA.
If I do this:
sXML = "<?xml version='1.0' encoding='utf-8'?>"
sXML = sXML & rest_of_xml_document
Print #iFile, sXML
then I get a file that is valid XML. However, if I change the "encoding=" to "utf-16", I get this error from my XML validator:
Switch from current encoding to specified encoding not supported.
Googling tells me that this means the xml encoding attribute is different to the ACTUAL encoding used by the file, hence I must be creating a utf-8 document via Open and Print commands.
If I do something like:
With CreateObject("ADODB.Stream")
.Type = 2
.Charset = "utf-16"
.Open
.WriteText sXML
.SaveToFile sFilename, 2
.Close
End With
then I end up with some funky characters (the BOM) at the beginning of my file which causes it to fail XML validation.
If I open the file in Notepad++, delete the BOM and change the Encoding to "UCS-2", then the file validates fine with a "utf-16" encoding value (meaning that UCS-2 is close enough to UTF-16 that it doesnt matter, or that XML is able to Switch from current encoding
between these two types.
I need to use UTF-16 because UTF-8 doesn't cover all the characters used in the presentations I'm exporting.
The question:
How can I get VBA to behave like Notepad++, creating a UTF-16-encoded text file without a BOM that can be filled with XML data? ANY help much appreciated!