INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...These forums are an excellent source and example of the way people can help each other..."
Geography
Where in the world do Tek-Tips members come from?
|
Save string as text file (with UTF8 encoding)
|
|
Hi fellow programmers,
I have a string, that I need to save in a text file that needs to be encoded with UTF8. But I don't know hot do do this?
Best regards
Morten Scheibye
|
|
see thread222-1090569 as that same issue has two solutions there. One using ADO STREAMS object and the other one using API's Regards
Frederico Fonseca SysSoft Integrated Ltd www.syssoft-int.com |
|
Hi, When trying to use the code mentioned in " thread222-1090569" (the ADO stream) I get the compile-error: "User-defined type not defined" in the declaration of "Dim adoStream As Stream". Do I need add a reference (I'm trying to run the code from Access 2003 VBA) ? Best regards 29466999 |
|
I have added a reference to "Microsoft Data Access Componenents Installed Version" (which according to the IT dep. should be version 2.8), but I still get the error. Is it the wrong reference or? I might want to use the AToW function instead from thread222-1090569 instead. Slightly modified, I have tried the code below, but then the txt-file only contains an "O" ? ------------- Sub test() Dim s As String Dim teststring As String teststring = "Once upon a time" s = AToW(teststring, CP_UTF8) Open "C:\result.txt" For Binary As #1 Put #1, , s Close 1 End Sub ------------- Best regards MS (29466999) |
|
as you wish to SAVE as UTF you need to use WToA instead. Regarding the ADO show us the code you are using please. Regards
Frederico Fonseca SysSoft Integrated Ltd www.syssoft-int.com |
|
The actual reference that you want, if you have MDAC 2.8 installed, is 'Microsoft ActiveX Data Objects 2.8 Library' |
|
(note that you'll probably get a conflict error message when you try this because Access 2003 comes with a reference to the 2.1 library alreay configured - so you need to remove that reference first) |
|
Thanks guys for helping me :) Regarding the WToA track: My code now looks like the one below. I have set the DLL that are used in the functions MsoCpgFromLid, MsoMultiByteToWideChar and MsoWideCharToMultiByte to "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\mso.dll" because the DLL's suggedsted in the code, does not exist on my machine. When running Sub test, result.txt contains the following: "???????? " And clearly not the string I would like to see in the file. Is it because I'm refering to the wrong DLL or ? --- Sub test() Dim s As String Dim teststring As String teststring = "Once upon a time" s = WToA(teststring, CP_UTF8) Open "C:\result.txt" For Binary As #1 Put #1, , s Close 1 End Sub --- Regarding the ADO I'm using an exact copy of the ADO code mentioned in " thread222-1090569". I have added a reference to "Microsoft ActiveX Data Objects Recordset 2.8 Library" but I still get an error if I try to compile this piece of code: --- Sub test Dim adoStream As Stream End sub --- |
|
try CODE Dim adoStream As adodb.Stream Dim adoStreamOut As adodb.Stream Set adoStream = New adodb.Stream adoStream.Charset = "UTF-8" adoStream.Open adoStream.LoadFromFile "c:\vbcode\utf8\example.txt"
adoStream.Position = 0 Set adoStreamOut = New adodb.Stream adoStreamOut.Charset = "unicode" adoStreamOut.Open adoStreamOut.WriteText adoStream.ReadText adoStreamOut.SaveToFile "c:\vbcode\utf8\exampleout.txt", adSaveCreateOverWrite
Regards
Frederico Fonseca SysSoft Integrated Ltd www.syssoft-int.com |
|
No, not "Microsoft ActiveX Data Objects Recordset 2.8 Library"
Perhaps I wasn't clear enough when I said: 'Microsoft ActiveX Data Objects 2.8 Library'. Or perhaps this isn't on your system, in which case use the 2.7 or 2.6 versions |
|
Hi,
Finally I got it to work using the ADO method.
First of all I made the mistake of adding "Microsoft ActiveX Data Objects Recordset 2.8 Library" and not "Microsoft ActiveX Data Objects 2.8 Library".
Secondly I found out (got suspicious after looking in the registry) that adoStream.Charset had to be set to "utf-8" (lowercase) and not "UTF-8" in order for it to work. Don't know why that is - perhaps because I'm running a Danish version of Windows.
Thanks a lot for the help!
MS |
|
Glad to hear you got it working |
|
|
 |
|