Unicode string literals in VBA

后端 未结 3 1165
萌比男神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: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 Ype in Windows-1252

提交回复
热议问题