LoginSignup
13
12

More than 5 years have passed since last update.

文字化け対応StrConv

Last updated at Posted at 2015-06-03

文字化け対応StrConv

Windows7 以降「゜」「゛」が単独で現れる文字列をStrConvすると文字化けするようになっています。
また、SJISコードが無いUNICODE文字列を変換すると?になってしまいます。
回避策をいれたStrConvはこちら。

文字化け対応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

他のソースをごらんになりたい方はこちらへ
Excelを便利にする250以上の機能を体系化したアドインはこちらです。
「RelaxTools Addin」窓の杜大賞受賞ソフト
http://software.opensquare.net/relaxtools/

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12