INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Save string as text file (with UTF8 encoding)

Save string as text file (with UTF8 encoding)

(OP)
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

RE: Save string as text file (with UTF8 encoding)

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

RE: Save string as text file (with UTF8 encoding)

(OP)
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

RE: Save string as text file (with UTF8 encoding)

Yes you need to add a reference to MDAC 2.8 . If you don't have it get it from www.microsoft.com

Regards

Frederico Fonseca
SysSoft Integrated Ltd
www.syssoft-int.com

RE: Save string as text file (with UTF8 encoding)

(OP)
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)

RE: Save string as text file (with UTF8 encoding)

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

RE: Save string as text file (with UTF8 encoding)

The actual reference that you want, if you have MDAC 2.8 installed, is 'Microsoft ActiveX Data Objects 2.8 Library'

RE: Save string as text file (with UTF8 encoding)

(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)

RE: Save string as text file (with UTF8 encoding)

(OP)
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
---

RE: Save string as text file (with UTF8 encoding)

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

RE: Save string as text file (with UTF8 encoding)

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

RE: Save string as text file (with UTF8 encoding)

(OP)
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

RE: Save string as text file (with UTF8 encoding)

Glad to hear you got it working

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close