VBA 中的 Unicode 字符串文字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23678033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:09:24  来源:igfitidea点击:

Unicode string literals in VBA

stringvbaexcel-vbaunicode-literalsexcel

提问by

I would like to declare (in a VBA class module) some private constant strings that contain Japanese characters. Is there a way to construct Stringliterals (or combining literals in a way) that may be accepted as initializers in a Constdeclaration? i.e. something like:

我想声明(在 VBA 类模块中)一些包含日语字符的私有常量字符串。有没有办法构造String可以在Const声明中作为初始值设定项接受的文字(或以某种方式组合文字)?即类似:

Private Const MY_CONST = ...

or

或者

Private Const MY_CONST As String = ...

I use MS Excel v14.0.6112.5000 (MS Office Professional Plus 2010).

我使用 MS Excel v14.0.6112.5000 (MS Office Professional Plus 2010)。

What won't work:

什么不起作用

  • Pasting the Japanese chars directly in a string literal (e.g. ... = "変数") because the VBA editor will mess with the chars;
  • Using ChrW()or ChrW$()(e.g. ... = ChrW$(22793) & ChrW$(25968)), because function calls are not allowed in Constinitializers.
  • 将日语字符直接粘贴到字符串文字(例如... = "変数")中,因为 VBA 编辑器会弄乱字符;
  • 使用ChrW()ChrW$()(例如... = ChrW$(22793) & ChrW$(25968)),因为在Const初始化程序中不允许函数调用。

What I wouldn't like:

什么我不喜欢

  • Faking the Constby creating Private Property Getreturning 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).
  • Const通过创建Private Property Get返回字符串来伪造,因为每次我访问该属性时都会重新创建该字符串(另外,令人困惑和丑陋......但是,好吧,最后两件事是一个品味问题)。

采纳答案by David Zemens

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).

通过创建私有属性伪造 Const 返回字符串,因为每次访问属性时都会重新创建字符串(另外,令人困惑和丑陋......但是,好吧,最后两件事是一个品味问题)。

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 Letprocedure), and construct the string in the Class_Initializeevent:

虽然这仍然很难看,但创建一个只读属性(本质上Const,因为它没有Property Let过程),并在Class_Initialize事件中构造字符串:

'## 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

回答by z??

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

VBA 源文件的编码为 Windows-1252,不支持日语。

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 (?t), which is the BOM marker

使用记事本转换字符串:复制粘贴 unicode 字符串,将文件另存为 unicode(不是 utf-8)并以 ANSI 格式打开,然后将其复制粘贴到 VBA 编辑器中,不带前两个字符 (?t),这是 BOM 标记

Explanation

解释

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

変数是U+5909 U+6570在unicode里面,是0x09 0x59 0x70 0x65UTF-16LE(Windows unicode encoding),这个序列对应<tab>YpeWindows-1252