Unicode string literals in VBA

后端 未结 3 1159
萌比男神i
萌比男神i 2020-12-06 19:14

I would like to declare (in a VBA class module) some private constant strings that contain Japanese characters. Is there a way to construct String literals (or

相关标签:
3条回答
  • 2020-12-06 19:23

    Another approach is to use an Enum in combination with a function to provide VBA autocomplete based on friendly names. I prefer this method because it keeps all the Unicode definitions in one place, and uses the readable names throughout the rest of my project.

    ' List friendly names of Unicode characters
    Public Enum eUnicodeConst
        RightArrow
        LeftArrow
        Clock2
    End Enum
    
    '---------------------------------------------------------------------------------------
    ' Procedure : UniConst
    ' Author    : Adam Waller
    ' Date      : 7/7/2020
    ' Purpose   : Search for characters: https://emojipedia.org/
    '           : Look up UTF-16 Decimal value(s) from the following site:
    '           : http://www.fileformat.info/info/unicode/char/search.htm
    '---------------------------------------------------------------------------------------
    '
    Public Function UniConst(Text As eUnicodeConst) As String
        Select Case Text
            Case LeftArrow:     UniConst = ChrW(8592)
            Case RightArrow:    UniConst = ChrW(8594)
            Case Clock2:        UniConst = ChrW(55357) & ChrW(56657)
        End Select
    End Function
    

    Now in my code, I can just use the UniConst function anytime I need a Unicode character or snippet without having to look character codes.

    0 讨论(0)
  • 2020-12-06 19:27

    Faking the Const by creating Private Property Get returning the string, because the string will be recreated every time I access the property (plus, is confusing and ugly... but, okay, the last two things are rather a matter of taste).

    You need not recreate the string each time you access the property.

    While this is still ugly as a matter of taste, make a read-only property (essentially Const, since it doesn't have a Property Let procedure), and construct the string in the Class_Initialize event:

    '## CLASS MODULE
    Private pUnicodeString As String
    
    Sub Class_Initialize()
        pUnicodeString = ChrW(22793) & ChrW(25968)
    End Sub
    
    Property Get UnicodeString() As String
        UnicodeString = pUnicodeString
    End Property
    

    And then invoke it like:

    '## STANDARD MODULE
    Sub Test()
    Dim c As myClass
    Set c = New myClass
    
    [A1].Value = c.UnicodeString
    
    End Sub
    
    0 讨论(0)
  • 2020-12-06 19:31

    The encoding of VBA source file is Windows-1252, which does not support Japanese.

    You cannot change the encoding of the source file, so you have to write its binary equivalent and then convert it before using it

    Const str = vbTab & "Ype" ' I use vbTab because the VBA editor replaces tabulation with spaces
    '...
    ustr = StrConv(str, vbFromUnicode)
    'ustr value is now "変数"
    

    Use notepad to convert the string: copy-paste the unicode string, save the file as unicode (not utf-8) and open it as ANSI, then copy-paste it into the VBA editor without the first two characters (ÿþ), which is the BOM marker

    Explanation

    変数 is U+5909 U+6570 in unicode which is 0x09 0x59 0x70 0x65 in UTF-16LE (Windows unicode encoding), and this sequence corresponds to <tab>Ype in Windows-1252

    0 讨论(0)
提交回复
热议问题