使用 VBA 调整用户窗体及其控件的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24668368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:46:16  来源:igfitidea点击:

Resize Userform and its Controls with VBA

excelvbaexcel-vbauserform

提问by BillD

I am trying to resize a userform and its controls with VBA in order to accommodate different size monitors. Following is the code I am using which is based on Ron DeBruin's code (http://www.rondebruin.nl/mac/mac022.htm).

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

In essence, the code is designed to scale the userform's size and location together with all of its controls.

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

The problem is I'm getting an error (shown below) on execution

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

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

I tried replacing .Properties("Top")with .Topand I got the Object doesn't support this property or methoderror.

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

Mr. DeBruin's code makes since; but I am at a loss as to why it is not working. Any help would certainly be appreciated.

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

采纳答案by Cool Blue

Based on your last comment, here is some example code showing how to change the properties at run time, without accessing the VBIDE.VBProject object. Of course, these changes will not persist.

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