2014-07-10 112 views
1

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

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

的问题是我在执行

得到一个错误(如下图所示)
"Run-time error '-2147467259(80004005)': Method 'Properties' of object '_VBComponent' failed" 

我试着用.Top更换.Properties("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 
+1

您是否允许通过信任中心访问VBA项目对象模型?这是与对象模型一起工作所必需的。 – Gareth

+0

是的,它已启用。虽然我试图让DeBruin先生的代码正常工作,但最终我希望能够遍历工作簿中的所有用户表单,以便将它们全部缩放。一些影响:对于工作簿中的每个AppUserform .... – BillD

+0

我不确定在哪种情况下的问题,但会警告需要在每台用户表单上的计算机上手动启用对VBA项目对象模型的访问将被使用。 – Gareth

回答

0

根据您最后的评论,下面是一些示例代码,演示如何在运行时更改属性,而无需访问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 
+0

感谢您的编码帮助。它效果很好。我感谢你抽出时间。 (我很抱歉这么长时间回复;家人死亡。) – BillD