【问题标题】:Resize Userform and its Controls with VBA使用 VBA 调整用户窗体及其控件的大小
【发布时间】:2014-07-10 04:12:05
【问题描述】:

我正在尝试使用 VBA 调整用户窗体及其控件的大小,以适应不同大小的监视器。以下是我正在使用的代码,它基于 Ron DeBruin 的代码 (http://www.rondebruin.nl/mac/mac022.htm)。

本质上,代码旨在缩放用户窗体及其所有控件的大小和位置。

问题是我在执行时遇到错误(如下所示)

"Run-time error '-2147467259(80004005)': Method 'Properties' of object '_VBComponent' failed"

我尝试将.Properties("Top") 替换为.Top,但出现Object doesn't support this property or method 错误。

先生。 DeBruin 的代码从那时起;但我不知道为什么它不起作用。任何帮助都将不胜感激。

Sub ChangeUserFormAndControlsSize()
    Dim AppUserform As Object
    Dim FormControl As Object
    Dim NameUserform As String
    Dim SizeCoefficient As Single

    SizeCoefficient = wsControls.Range("SizeCoefficient")

    NameUserform = "form_APScheduler"

    Set AppUserform = ThisWorkbook.VBProject.VBComponents(NameUserform)
    With AppUserform
        .Properties("Top") = .Properties("Top") * SizeCoefficient   '*** ERROR OCCURS HERE
        .Properties("Left") = .Properties("Left") * SizeCoefficient
        .Properties("Height") = .Properties("Height") * SizeCoefficient
        .Properties("Width") = .Properties("Width") * SizeCoefficient
    End With

    For Each FormControl In AppUserform.Designer.Controls
        With FormControl
            .Top = .Top * SizeCoefficient
            .Left = .Left * SizeCoefficient
            .Width = .Width * SizeCoefficient
            .Height = .Height * SizeCoefficient

            On Error Resume Next
            .Font.Size = .Font.Size * SizeCoefficient
            On Error GoTo 0
        End With
    Next FormControl

End Sub

【问题讨论】:

  • 您是否启用了通过信任中心访问 VBA 项目对象模型?这是使用对象模型所必需的。
  • 是的,它已启用。虽然我试图让 DeBruin 先生的代码正常工作,但最终我希望能够遍历工作簿中的所有用户表单,以便对它们进行扩展。大意是:对于 Workbook 中的每个 AppUserform ....
  • 我不确定在哪种情况下会出现什么问题,但会警告说,需要在将使用用户表单的每台计算机上手动启用对 VBA 项目对象模型的访问。
  • 我可以毫无错误地运行该代码。如果你在 With AppUserform 设置了一个 break 并为 AppUserform 设置了一个 watch,你会看到什么?对象正常吗?另外,此时 SizeCoefficient 还可以吗?
  • 我在 AppUserform 上设置了手表。我看到了什么?那里有很多信息(请原谅我的无知),但看起来很有趣的一件事是“名称:“form_APScheduler”:字符串:Module1.ChangeUserFormAndControlsSize”。名字是正确的。对象正常吗?我不确定要寻找什么来回答这个问题。 SizeCoefficient 可以吗?是的;该变量仍然具有正确的值(在这种情况下为 0.75)。问题:这是否可以通过其他方式完成,即不访问 VBA 项目对象模型?

标签: vba excel userform


【解决方案1】:

根据您的最后评论,这里有一些示例代码,展示了如何在运行时更改属性,而无需访问 VBIDE.VBProject 对象。当然,这些变化不会持续。

Option Explicit
Sub testForm()
Dim UF As form_APScheduler
Dim FormControl As MSForms.Control
Dim SizeCoefficient As Double

    SizeCoefficient = inputNumber("Scale Factor: ", "Form", 1)
    Set UF = New form_APScheduler
    With UF
        .Top = .Top * SizeCoefficient
        .Left = .Left * SizeCoefficient
        .Width = .Width * SizeCoefficient
        .Height = .Height * SizeCoefficient
    End With
    For Each FormControl In UF.Controls
        With FormControl
            .Top = .Top * SizeCoefficient
            .Left = .Left * SizeCoefficient
            .Width = .Width * SizeCoefficient
            .Height = .Height * SizeCoefficient

            On Error Resume Next
            .Font.Size = .Font.Size * SizeCoefficient
            On Error GoTo 0
        End With
    Next FormControl
    UF.Show
    Unload UF
End Sub
Function inputNumber(prompt As String, title As String, defValue As Variant) As Variant
    inputNumber = Application.InputBox(prompt, title, defValue, , , , , 1)
End Function

【讨论】:

  • 感谢编码帮助。它工作得很好。感谢您抽出宝贵的时间。 (我很抱歉花了这么长时间才回复;家人去世了。)
猜你喜欢
  • 2013-04-30
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 2011-03-30
  • 2023-04-10
相关资源
最近更新 更多