如何在VBA中更改用户窗体的标题栏文本?

发布于 2024-09-04 01:19:13 字数 1081 浏览 12 评论 0原文

我正在维护一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2024-09-11 01:19:13

您的 frmFoo 实际上与基本 UserForm 的类型不同,而是在 VBA 的怪异 OO 实现中从内部“派生”而来,因此您不能将其可靠地用作参数类型,使用Object代替即可;

Sub TranslateDialog(pForm As Object, pFormName As String)

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;

Sub TranslateDialog(pForm As Object, pFormName As String)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文