Internally, all VB strings are in Unicode format anyway. The "put" statement converts strings to ANSI *unless* you pass the string as a byte array.
The StrConv method that Giorgio provides will work but includes much extra (unnecessary) overhead because you're essentially converting the VB unicode string (2 bytes per character) to a 4-byte per character string thus using much more memory and requiring extra processing.
Here's the better way using the byte array mentioned above:
Sub WriteUnicodeTextFile(ByRef Path As String, _ ByRef Value As String)
Dim Buffer() As Byte Dim FileNum As Integer
' Convert string to an array of bytes, preserving unicode (2bytes per character) Buffer = Value
FileNum = FreeFile Open Path For Binary As FileNum Put FileNum, , Buffer Close FileNum
And one more - from Tony Jollans: Set MyStream = CreateObject("ADODB.Stream") With MyStream .Type = adTypeText .Charset = "Unicode" .Open .WriteText r!Captn ' your foreign unicode text .SaveToFile "C:\whatever.txt" .Close End With
5 Comments:
Internally, all VB strings are in Unicode format anyway. The "put" statement converts strings to ANSI *unless* you pass the string as a byte array.
The StrConv method that Giorgio provides will work but includes much extra (unnecessary) overhead because you're essentially converting the VB unicode string (2 bytes per character) to a 4-byte per character string thus using much more memory and requiring extra processing.
Here's the better way using the byte array mentioned above:
Sub WriteUnicodeTextFile(ByRef Path As String, _
ByRef Value As String)
Dim Buffer() As Byte
Dim FileNum As Integer
' Convert string to an array of bytes, preserving unicode (2bytes per character)
Buffer = Value
FileNum = FreeFile
Open Path For Binary As FileNum
Put FileNum, , Buffer
Close FileNum
End Sub
Wayne Phillips
http://www.everythingaccess.com
Thanks for update, Wayne!
This worked a treat, thanks to you both.
And one more - from Tony Jollans:
Set MyStream = CreateObject("ADODB.Stream")
With MyStream
.Type = adTypeText
.Charset = "Unicode"
.Open
.WriteText r!Captn ' your foreign unicode text
.SaveToFile "C:\whatever.txt"
.Close
End With
Great job! Thanks!
Post a Comment
<< Home