将 unicode 字符插入到 VBA 字符串中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15642786/
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-11 20:16:37  来源:igfitidea点击:

insert unicode character into a VBA string

vbaunicode

提问by Commata

Using MS Access 2003 on Windows 7, I found that the following function strips all accents from ANSI strings, changing (for example) se?or to senor:

在 Windows 7 上使用 MS Access 2003,我发现以下函数从 ANSI 字符串中去除了所有重音符号,将(例如)se?or 更改为 senor:

Public Function RemoveAccents(ByVal inputString As String) As String
Const accentString As String = "àá?????èéê?ìí????òó???ùú?üYàáa????èéê?ìí??e?òóó???ùú?üy?"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaaceeeeiiiionoooooouuuuyy"
Dim i As Integer

For i = 1 To Len(accentString)
inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
Next i
RemoveAccents = inputString
End Function

But when I tried to add Latin small letter C with Caron (U-010D)(č)(HTML č) to the function Const accentString, like this,

但是当我尝试将带有 Caron (U-010D)(č)(HTML č) 的拉丁小写字母 C 添加到函数 Const AccentString 时,就像这样,

Const accentString As String = "àá?????èéê?ìí????òó???ùú?üYàáa????" & ChrW$(&H10D) & "èéê?ìí??e?òóó???ùú?üy?"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"

I was unable to run the function. Is there a syntax which will allow me to adapt this function to strip the diacriticals from strings containing characters not in the ANSI character set?

我无法运行该功能。是否有一种语法可以让我调整此函数以从包含不在 ANSI 字符集中的字符的字符串中去除变音符号?

回答by David Zemens

You can't have functions like ChrW()in a constant declaration.

你不能像ChrW()常量声明那样使用函数。

Revised

修改

If you can make these Public variables instead of Constants, then this should take care of it for you:

如果您可以创建这些公共变量而不是常量,那么这应该为您处理:

Const cWithCaron As String = &H10D
Public accentString As String
Public nonAccentStr As String


Sub TestStrings()
Dim clnString As String

    accentString = "àá?????èéê?ìí????òó???ùú?üYàáa????" & ChrW(cWithCaron) & "èéê?ìí??e?òóó???ùú?üy?"

    nonAccentStr = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"
    'I added this variable to test the function:
    clnString = RemoveAccents(accentString)
    'And a message box to display the results:
    MsgBox clnString = nonAccentStr

End Sub

Public Function RemoveAccents(ByVal inputString As String) As String

Dim i As Integer

    For i = 1 To Len(accentString)

        inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
    Next i

    RemoveAccents = inputString

End Function