Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save string as text file (with UTF8 encoding) 2

Status
Not open for further replies.

29466999

Programmer
Joined
Nov 22, 2002
Messages
13
Location
DK
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
 
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)
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top