Option Explicit
Public Const adModeReadWrite = 3
Public Const adTypeBinary = 1
Public Const adTypeText = 2
'  Inspiré de BodwadUK : http://www.vbforums.com/showthread.php?674670-utf-8-string-converter
Public Function Utf8_Encode(ByRef txt As String) As String
Dim Data() As Byte, s As String, i As Long
'
    With CreateObject("ADODB.Stream")   ' init stream
        .Charset = "utf-8"
        .Mode = adModeReadWrite
        .Type = adTypeText
        .Open
        .WriteText txt                  ' write bytes into stream
        .Flush
        .Position = 0                   ' rewind stream and read text
        .Type = adTypeBinary
        .Read 3                         ' skip first 3 bytes as this is the utf-8 marker
        Data = .Read()
        .Close                          ' close up and return
    End With
        
    For i = 0 To UBound(Data)           ' Convert back to ascii
        s = s & Chr(Data(i))
    Next i
    Utf8_Encode = s
End Function
'
Function Utf8_Decode(ByVal txt As String) As String
Dim ln As Long, s As String, i As Integer, j As Integer, K As Integer
    For ln = 1 To Len(txt)
        i = Asc(Mid(txt, ln, 1))
        If i > 127 Then
            If Not i And 32 Then
            j = Asc(Mid(txt, ln + 1, 1))
            s = s & ChrW$(((31 And i) * 64 + (63 And j)))
            ln = ln + 1
        Else
            j = Asc(Mid(txt, ln + 1, 1))
            K = Asc(Mid(txt, ln + 2, 1))
            s = s & ChrW$(((i And 15) * 16 * 256) + ((j And 63) * 64) + (K And 63))
            ln = ln + 2
        End If
            Else
            s = s & Chr$(i)
        End If
    Next ln
    Utf8_Decode = s
End Function

