文字化け対応StrConv

Windows7 になってから「゜」「゛」が文字化けするようになってたみたいですね。
改良版のStrConvを作成したのでアップしてみます。

'文字化け対応StrConv(vbUnicode, vbFromUnicodeは使えません)
Public Function StrConvU(ByVal strSource As String, conv As VbStrConv) As String

    Dim i As Long
    Dim strBuf As String
    Dim c As String
    Dim strRet As String
    Dim strBefore As String
    Dim strChr As String

    strRet = ""
    strBuf = ""
    strBefore = ""

    For i = 1 To Len(strSource)

        c = Mid(strSource, i, 1)

        Select Case c
            '全角の濁点、半濁点
            Case "゜", "゛"
                If (conv And vbNarrow) > 0 Then
                    If c = "゜" Then
                        strChr = "゚"
                    Else
                        strChr = "゙"
                    End If
                Else
                    strChr = c
                End If
                strRet = strRet & strConv(strBuf, conv) & strChr
                strBuf = ""
                
            '半角の半濁点
            Case "゚"
                '1つ前の文字
                Select Case strBefore
                    Case "ハ" To "ホ"
                        strBuf = strBuf & c
                    Case Else
                        If (conv And vbWide) > 0 Then
                             strChr = "゜"
                        Else
                            strChr = c
                        End If
                        strRet = strRet & strConv(strBuf, conv) & strChr
                        strBuf = ""
                End Select
                
            '半角の濁点
            Case "゙"
                '1つ前の文字
                Select Case strBefore
                    Case "カ" To "コ", "サ" To "ソ", "タ" To "ト", "ハ" To "ホ"
                        strBuf = strBuf & c
                    Case Else
                        If (conv And vbWide) > 0 Then
                            strChr = "゛"
                        Else
                            strChr = c
                        End If
                        strRet = strRet & strConv(strBuf, conv) & strChr
                        strBuf = ""
                End Select
                
            'その他
            Case Else
                '第二水準等StrConvで文字化けするものを退避
                If Asc(c) = 63 And c <> "?" Then
                    strRet = strRet & strConv(strBuf, conv) & c
                    strBuf = ""
                Else
                    strBuf = strBuf & c
                End If
        End Select
        
        '1個前の文字
        strBefore = c

    Next

    If strBuf <> "" Then
        strRet = strRet & strConv(strBuf, conv)
    End If

    StrConvU = strRet

End Function

ABOUTこの記事をかいた人

はてなブックマークで驚愕の1600越えを記録した伝説が今明らかに! エクセル方眼紙 四天王の1人(ほぼ最弱)窓の杜大賞2014 大賞受賞! Excelを便利にする250以上の機能を体系化したアドインはこちらです。