Unicode and VBA

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anantvrana@yahoo.com

    #1

    Unicode and VBA

    Hello All,

    I am trying to read Unicode (Kanji character) data from a text file.
    When I store unicode data into variable my Kanji character gets messed
    up.

    I am using following code

    Open File1 For Input Access Read As #1
    While Not EOF(1)
    Line Input #1, data ' each line has English + Kanji characters
    data
    'Debug.Print data

    Can some one help or guide to store English + Kanji (Unicode) data.

    Thank you,
    AVR

  • polite person

    #2
    Re: Unicode and VBA

    On 18 Nov 2005 15:39:54 -0800, anantvrana@yaho o.com wrote:
    [color=blue]
    >Hello All,
    >
    >I am trying to read Unicode (Kanji character) data from a text file.
    >When I store unicode data into variable my Kanji character gets messed
    >up.
    >
    >I am using following code
    >
    >Open File1 For Input Access Read As #1
    >While Not EOF(1)
    > Line Input #1, data ' each line has English + Kanji characters
    >data
    > 'Debug.Print data
    >
    >Can some one help or guide to store English + Kanji (Unicode) data.
    >
    >Thank you,
    >AVR[/color]

    What are you going to do with the codes? Doing anything with unicode in vba is hard work.
    Here is one way of reading a unicode text file into a byte array. Each unicode char is a pair of
    bytes, low order byte first.

    For inf on FSO see http://www.aivosto.com/visdev/fso.html

    For inf on how VB stores strings see
    http://msdn.microsoft.com/library/de...og6_topic1.asp
    from which the function BSTRtoLPWSTR is taken.

    Option Compare Database
    Option Explicit

    Declare Sub CopyMemory Lib "kernel32" _
    Alias "RtlMoveMem ory" (lpDest As Any, _
    lpSource As Any, ByVal cbCopy As Long)

    Public Sub aaa()
    'read unicode file into byte array b

    Dim myFSO As New Scripting.FileS ystemObject
    Dim ts As TextStream
    Dim alltext As String
    Dim b() As Byte
    Dim lpsz As Long, lng As Long

    Set ts = myFSO.OpenTextF ile("c:\Hindi.t xt", ForReading, , TristateTrue)
    'TriStateTrue opens the file as Unicode.
    alltext = ts.ReadAll

    lng = BSTRtoLPWSTR(al ltext, b, lpsz)
    'lng is number of bytes

    'now do what you need to do with the bytes

    End Sub


    Function BSTRtoLPWSTR(sB STR As String, b() As Byte, lpwsz As Long) As Long

    ' Input: a nonempty BSTR string
    ' Input: **undimensioned ** byte array b()
    ' Output: Fills byte array b() with Unicode char string from sBSTR
    ' Output: Fills lpwsz with a pointer to b() array
    ' Returns byte count, not including terminating 2-byte Unicode null character
    ' Original BSTR is not affected

    Dim cBytes As Long

    cBytes = LenB(sBSTR)

    ' ReDim array, with space for terminating null
    ReDim b(1 To cBytes + 2) As Byte

    ' Point to BSTR char array
    lpwsz = StrPtr(sBSTR)

    ' Copy the array
    CopyMemory b(1), ByVal lpwsz, cBytes + 2

    ' Point lpsz to new array
    lpwsz = VarPtr(b(1))

    ' Return byte count
    BSTRtoLPWSTR = cBytes

    End Function





    Comment

    Working...