如何在VBA中显示带有Unicode字符的消息框? [英] How do I display a messagebox with unicode characters in VBA?

查看:224
本文介绍了如何在VBA中显示带有Unicode字符的消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBA中有一个包含Unicode字符的字符串.

I have a string containing unicode characters in VBA.

我想在包含该字符串的消息框中显示该字符串.

I want to display that string in a message box containing it.

但是,消息框仅包含一个问号,而不是字符串.

However, instead of the string, the message box only contains a questionmark.

MCVE:

Dim s As String
s = ChrW(5123)
MsgBox s

推荐答案

MsgBox与非ANSI Unicode字符不兼容.

MsgBox is not compatible with non-ANSI unicode characters.

但是,我们可以使用WinAPI MessageBoxW功能显示消息框.

We can display message boxes with the WinAPI MessageBoxW function, however, and that is .

让我们声明该函数,然后为其创建一个与VBA MsgBox函数几乎相同的包装器:

Let's declare that function, and then create a wrapper for it that's nearly identical to the VBA MsgBox function:

Private Declare PtrSafe Function MessageBoxW Lib "User32" (ByVal hWnd As LongPtr, ByVal lpText As LongPtr, ByVal lpCaption As LongPtr, ByVal uType As Long) As Long

Public Function MsgBoxW(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String = "Microsoft Access") As VbMsgBoxResult
    Prompt = Prompt & VbNullChar 'Add null terminators
    Title = Title & vbNullChar 
    MsgBoxW = MessageBoxW(Application.hWndAccessApp, StrPtr(Prompt), StrPtr(Title), Buttons)
End Function

此功能仅与Microsoft Access兼容.但是,对于Excel,您可以将Application.hWndAccessAppApplication.hWnd交换以使其工作.对于其他与VBA兼容的应用程序,您必须找到获取hWnd的适当方法.

This function is only compatible with Microsoft Access. However, for Excel you can swap Application.hWndAccessApp with Application.hWnd to make it work. For other VBA compatible applications, you'll have to find the appropriate way to get the hWnd.

只要不使用上下文相关的帮助功能,就可以像MsgBox一样使用它:

You can use it like MsgBox, as long as you don't use the context-dependent help functionality:

Dim s As String
s = ChrW(5123)
MsgBoxW s

这篇关于如何在VBA中显示带有Unicode字符的消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆