How to encode a UTF8 file in VBA?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, i'm trying to change the encoding of a text file from unicode to UTF8
in a the Access module. can't figure out if there's an object & method would
allow me to do it, or if scripting would help - have been searching for days
on the net but couldn't find the answer. Please help...
 
This isn't something I've used in the 'real world', but from my reading of
the article at the URL below, I believe it should be possible to do this
using the ADO Stream object.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthreadvbx.asp

I tested using the following code. It runs without error. But I'm not sure
how to determine that the resulting file is, in fact, a UTF-8 file. (I know
it can be done by examining the file using a HEX editor, but I don't
currently have one installed).

Public Sub SaveStream()

Dim objStream As Stream

Set objStream = New Stream
objStream.Open
objStream.Position = 0
objStream.Charset = "UTF-8"
objStream.WriteText "A string of text"
objStream.SaveToFile "c:\dsdata\mightbeutf8.txt", adSaveCreateOverWrite
objStream.Close

End Sub
 
Figured out a way to test it. Use the code to write an XML file including
the encoding attribute, and open the XML file in Internet Explorer. Internet
Explorer will complain if the encoding of the XML file doesn't match the
encoding attribute, so if Internet Explorer doesn't complain (and it
doesn't) it worked! :-)

Public Sub SaveStream()

Dim objStream As Stream
Dim strXML As String

strXML = "<?xml version='1.0' encoding='UTF-8'?> " & _
"<dataroot>" & _
"<tblTest>" & _
"<TestID>1</TestID>" & _
"</tblTest>" & _
"</dataroot>"

Set objStream = New Stream
objStream.Open
objStream.Position = 0
objStream.Charset = "UTF-8"
objStream.WriteText strXML
objStream.SaveToFile "c:\dsdata\mightbeutf8.xml", adSaveCreateOverWrite
objStream.Close

End Sub

Just for laughs (or more seriously, to demonstrate that Internet Explorer
really would complain if the result wasn't a UTF-8 file) try changing the
encoding attribute to 'UTF-16' without also changing the objStream.Charset,
run the code again, and see what happens when you try to open the XML file
in Internet Explorer.
 
Back
Top