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

我正在维护一个使用VBA for Excel 2002(XP)/ 2003编写的旧应用程序,并试图将其国际化。

为此,我dynamic读取翻译的string,并通过更新.Caption属性更新我的用户窗体上的各种控件。

这符合所有控件的预期效果,但不是表单本身 – 当我更改表单的.Caption属性时,标题栏继续显示“硬编码”值,而新值则显示在其下方窗体本身的“canvas”的顶部。

是否可以更改显示后的用户窗体的标题栏文本,还是必须更改表单的.Caption属性才能显示,以便它被反映在标题栏而不是canvas/客户区?

我的代码看起来像这样:

' 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。

您的frmFoo实际上与基本的UserForm不是相同的types,而是在VBA的frmFoo的OO实现中内部“下降”的,所以您不能将其作为参数types可靠地使用,而使用Object将会起作用。

 Sub TranslateDialog(pForm As Object, pFormName As String)