INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

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!

E-mail*
Handle

Password
Verify P'word
*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
Partner Button
(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)

29466999 (Programmer)
14 Jul 05 4:59
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
Check Out Our Whitepaper Library. Click Here.
Helpful Member!fredericofonseca (IS/IT--Management)
14 Jul 05 5:31
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

29466999 (Programmer)
14 Jul 05 5:45
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
Helpful Member!fredericofonseca (IS/IT--Management)
14 Jul 05 6:04
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

29466999 (Programmer)
14 Jul 05 7:09
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)
Helpful Member!fredericofonseca (IS/IT--Management)
14 Jul 05 7:20
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

Helpful Member!strongm (MIS)
14 Jul 05 7:29
The actual reference that you want, if you have MDAC 2.8 installed, is 'Microsoft ActiveX Data Objects 2.8 Library'
Helpful Member!strongm (MIS)
14 Jul 05 7:30
(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)
29466999 (Programmer)
14 Jul 05 8:51
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
---
Helpful Member!fredericofonseca (IS/IT--Management)
14 Jul 05 9:10
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

Helpful Member!strongm (MIS)
14 Jul 05 9:36
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
29466999 (Programmer)
14 Jul 05 10:41
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
Helpful Member!strongm (MIS)
14 Jul 05 11:02
Glad to hear you got it working

Start A New Thread

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

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

Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

LINK TO THIS FORUM!
(Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum)
TITLE: Visual Basic(Microsoft): Version 5 & 6 Forum at Tek-Tips
URL: http://www.tek-tips.com/threadminder.cfm?pid=222
DESCRIPTION: Visual Basic(Microsoft): Version 5 & 6 technical support forum and mutual help system for computer professionals. Selling and recruiting forbidden.