VBA - Convert string to UNICODE

前端 未结 2 1123
南方客
南方客 2020-12-01 20:21

I need to convert the string HTML from a mix of Cyrillic and Latin symbols to UNICODE.

I tried the following:

Public HTML As String
    Sub HTMLsearc         


        
2条回答
  •  隐瞒了意图╮
    2020-12-01 20:40

    My production order data comes from many countries. this is the only vba function I could find that really works.

    Private Const CP_UTF8 = 65001
    
    Private Declare Function MultiByteToWideChar Lib "kernel32" ( _
       ByVal CodePage As Long, ByVal dwFlags As Long, _
       ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
       ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    
    
    Public Function sUTF8ToUni(bySrc() As Byte) As String
       ' Converts a UTF-8 byte array to a Unicode string
       Dim lBytes As Long, lNC As Long, lRet As Long
    
       lBytes = UBound(bySrc) - LBound(bySrc) + 1
       lNC = lBytes
       sUTF8ToUni = String$(lNC, Chr(0))
       lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bySrc(LBound(bySrc))), lBytes, StrPtr(sUTF8ToUni), lNC)
       sUTF8ToUni = Left$(sUTF8ToUni, lRet)
    End Function
    

    Example Usage:

    Dim sHTML As String
    Dim bHTML() As Byte
    bHTML = GetHTML("http://yoururlhere/myorderdata.php")
    sHTML = sUTF8ToUni(bHTML)
    sHTML = Mid(sHTML, 2)  'strip off Byte Order Mark: EF BB BF
    

提交回复
热议问题