View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Petr Bazant[_2_] Petr Bazant[_2_] is offline
external usenet poster
 
Posts: 2
Default Convert string from one codepage to another

On May 14, 4:45 am, "NickHK" wrote:
Petr,
StrConv maybe .

NickHK

"Petr Bazant" wrote in message

ups.com...
Can someone please give me an advise, how to convert in VBA e.g. this
string "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU" which comes from DOS 852codepageto UTF-8 or to Windows-1250codepage. I was trying to use
e.g. WideCharToMultiByte but I am not experienced enough to do it. BTW
the string should correctly look like this "VYÚCTOVÁNÍ CÁSTI
SOUD.POPLATKU".


StrConv does not work directly. The solution (not from my head) is:

Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, _
ByVal lpWideCharStr As String, ByVal cchWideChar As Long) As
Long
Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
ByVal CodePage As Long, ByVal dwFlags As Long, _
ByVal lpWideCharStr As String, ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, _
ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As
Long) As Long
Private Const MB_PRECOMPOSED = &H1
Private Const MY_CP_WINDOWS As Long = 1250
Private Const MY_CP_ISO_1 As Long = 28591
Private Const MY_CP_ISO_2 As Long = 28592 ' ISO Latin-2
Private Const MY_CP_IBM_852 As Long = 852


Private Function StrConvCP1ToCP2(sStr As String, lFromCP As Long, _
Optional lToCP As Long = 0) As String
Dim sStrW As String

sStrW = String$(2 * Len(sStr), vbNullChar)
MultiByteToWideChar lFromCP, MB_PRECOMPOSED, sStr, Len(sStr), _
sStrW, Len(sStr)
If lToCP = 0 Then
sStr = StrConv(sStrW, vbFromUnicode)
Else
WideCharToMultiByte lToCP, 0&, sStrW, Len(sStr), sStr,
Len(sStr), 0&, 0&
End If

StrConvCP1ToCP2 = sStr

End Function

Maybe someone will need it.

Sub test()
Dim out As String
Const src As String = "VYé¬TOVµNÖ ¬µSTI SOUD.POPLATKU"
out = StrConvCP1ToCP2(src, 852, 1250)
End Sub