如何在VBA中更改用户窗体的标题栏文本?
我正在维护一个用 VBA 为 Excel 2002 (XP)/2003 编写的旧应用程序,并尝试将其国际化。
为此,我动态读取翻译后的字符串,并通过更新用户窗体上的各种控件的 .Caption 属性来更新它们。
这对所有控件都按预期工作,但不适用于表单本身 - 当我更改表单的 .Caption 属性时,标题栏将继续显示“硬编码”值,而新值将显示在其正下方,位于表单本身“画布”的顶部。
是否可以在显示后更改用户窗体的标题栏文本,或者我是否必须在显示之前更改窗体的 .Caption 属性,以便它反映在标题栏中而不是反映在画布/客户区?
我的代码看起来像这样:
' 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
正如我所说,对 pForm.Caption 的赋值确实有效果 - 但它不会写入窗口的标题栏,而是直接写入窗口的标题栏下方。我在 Windows XP SP 3 上运行 Excel 2003。
I'm maintaining an old-ish application written in VBA for Excel 2002 (XP)/2003, and am trying to internationalise it.
To do this, I read in the translated strings dynamically and update the various controls on my userform by updating their .Caption property.
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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
frmFoo
实际上与基本UserForm
的类型不同,而是在 VBA 的怪异 OO 实现中从内部“派生”而来,因此您不能将其可靠地用作参数类型,使用Object代替即可;Your
frmFoo
is not actually the same type as the baseUserForm
, 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;