如何在 VBA 中更改用户表单的标题栏文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2972885/
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
How to change the title bar text of a Userform in VBA?
提问by Philip Newton
I'm maintaining an old-ish application written in VBA for Excel 2002 (XP)/2003, and am trying to internationalise it.
我正在维护一个用 VBA 编写的用于 Excel 2002 (XP)/2003 的老式应用程序,并试图将其国际化。
To do this, I read in the translated strings dynamically and update the various controls on my userform by updating their .Caption property.
为此,我动态读取翻译后的字符串,并通过更新它们的 .Caption 属性来更新我的用户表单上的各种控件。
This works as expected for all controls but not for the form itself -- when I change the form's .Caption property, then the title bar keeps displaying the "hard-coded" value, and the new value is instead displayed just below it, at the top of the "canvas" of the form itself.
这适用于所有控件,但不适用于表单本身——当我更改表单的 .Caption 属性时,标题栏会继续显示“硬编码”值,而新值则显示在它的正下方,在表单本身“画布”的顶部。
Is it possible to change the title bar text of a UserForm after it has been shown, or do I have to change the .Caption property of the form before it is shown in order for it to be reflected in the title bar rather than in the canvas/client area?
是否可以在显示后更改用户窗体的标题栏文本,或者我是否必须在显示之前更改窗体的 .Caption 属性才能使其反映在标题栏中而不是在画布/客户区?
My code looks something like this:
我的代码看起来像这样:
' in frmFoo
Private Sub UserForm_Activate()
' ...
TranslateDialog Me, "frmFoo"
' ...
End Sub
' in a VBA module
Sub TranslateDialog(pForm As UserForm, pFormName As String)
Dim new Caption As String
Const notFound As String = "###!!@@NOTFOUND@@!!###"
' ...
' GetMessage() returns the translated message for a given key, or the
' default value (second parameter) if no translation is available.
' The translation key for the form caption is the form name itself.
newCaption = GetMessage(pFormName, notFound)
If newCaption <> notFound Then pForm.Caption = newCaption
' ...
End Sub
As I said, the assignment to pForm.Caption
does have an effect - but it doesn't write to the title bar of the window, but rather directly beneath it. I'm running Excel 2003 on Windows XP SP 3.
正如我所说,分配给pForm.Caption
确实有效果 - 但它不会写入窗口的标题栏,而是直接写入窗口的下方。我在 Windows XP SP 3 上运行 Excel 2003。
采纳答案by Alex K.
Your frmFoo
is not actually the same type as the base UserForm
, rather it's internally "descended" from it in VBA's wierd OO implementation so you can't use that reliably as a parameter type, using Object instead will work;
您frmFoo
实际上与 base 的类型不同UserForm
,而是在 VBA 的奇怪 OO 实现中从它内部“下降”,因此您不能可靠地将其用作参数类型,而是使用 Object 将起作用;
Sub TranslateDialog(pForm As Object, pFormName As String)