Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / September 2005

Tip: Looking for answers? Try searching our database.

How to encode a UTF8 file in VBA?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wc - 21 Sep 2005 18:19 GMT
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...
Brendan Reynolds - 23 Sep 2005 23:50 GMT
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/mdmt
hreadvbx.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

Signature

Brendan Reynolds (MVP)

> Hello, i'm trying to change the encoding of a text file from unicode to
> UTF8
[quoted text clipped - 3 lines]
> days
> on the net but couldn't find the answer. Please help...
Brendan Reynolds - 24 Sep 2005 13:24 GMT
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.

Signature

Brendan Reynolds (MVP)

> 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
[quoted text clipped - 28 lines]
>> days
>> on the net but couldn't find the answer. Please help...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.