VBA MSFORMS vs Controls - 差异如何

TYa*_*ale 5 vba userform

向用户窗体添加控件时,以下内容之间有何区别.我很困惑何时适合使用其中任何一个.

Dim aButton1 as MSFORMS.CommandButton
Dim aButton2 as Control.CommandButton
Dim aButton3 as CommandButton
Run Code Online (Sandbox Code Playgroud)

dee*_*dee 13

首先添加UserForm.然后在VBA IDE中按F2,将出现对象浏览器.在左上角是组合框,选择MSForms.在类列表中,您可以看到属于MSForms对象库的类.

您可以在该列表中看到CommandButtonControl:

在此输入图像描述

要在代码中声明CommandButton类型的变量:

Dim button1 As MSForms.CommandButton
Dim button2 As CommandButton
Run Code Online (Sandbox Code Playgroud)

变量button1肯定是来自MSForms的CommandButton类型.该BUTTON2可以在您的本地VBA项目定义自己的类 ...但如果当地的VBA项目不包含这样的名字就会从MSForms被视为以及任何类.但是,如果您引用另一个对象库,说'MSFoo',它将包含类CommandButton,您将必须像这样声明它们完全限定:

Dim button1 As MSForms.CommandButton
Dim button2 As MSFoo.CommandButton
Run Code Online (Sandbox Code Playgroud)

要在代码中声明Control类型的变量:

Dim controlObject As MSForms.Control
Run Code Online (Sandbox Code Playgroud)

使用Control类型的变量作为控件的基类类型.例如,以控制集合:

For Each controlObject In Me.Controls
    Debug.Print VBA.TypeName(controlObject)
Next controlObject
Run Code Online (Sandbox Code Playgroud)

或者作为函数中的参数,它不仅需要一种控件:

Private Sub PrinControlName(ByRef c As MSForms.Control)
    Debug.Print c.Name
End Sub
Run Code Online (Sandbox Code Playgroud)

因此,我认为使用像MSForms.CommandButton这样的完全限定名称是合适的.使用Control.CommandButton是错误的,并且在您使用类CommandButton引用一个名为"Control"的对象库之前不会编译.

编辑:

这里是本地创建的具有相同名称的类的示例,如MSForm.CommandButton: 在此输入图像描述

在这种情况下,TypeNameTypeOf如何工作:

Option Explicit

Private m_buttonMsForms As MSForms.CommandButton
Private m_buttonLocal As CommandButton

Private Sub UserForm_Initialize()
    Set m_buttonMsForms = Me.Controls.Add( _
        "Forms.CommandButton.1", "testMsButton", True)
    Set m_buttonLocal = New CommandButton

    m_buttonLocal.Name = "testLocalButton"

    Debug.Print "We have two instances of two different button types: " & _
        m_buttonLocal.Name & " and " & m_buttonMsForms.Name

    ' Check instances with TypeName function
    ' TypeName function returns same results
    If VBA.TypeName(m_buttonMsForms) = VBA.TypeName(m_buttonLocal) Then
        Debug.Print "TypeName of both buton types returns same result"
    End If

    ' Check instances with TypeOf operator
    ' TypeOf doen't work with not initialised objects
    If m_buttonLocal Is Nothing Or m_buttonMsForms Is Nothing Then _
        Exit Sub

    ' TypeOf operator can distinguish between
    ' localy declared CommandButton type and MSForms CommandButton
    If TypeOf m_buttonLocal Is MSForms.CommandButton Then _
        Debug.Print "m_buttonLocal Is MSForms.CommandButton"

    If TypeOf m_buttonMsForms Is CommandButton Then _
        Debug.Print "m_buttonMsForms Is CommandButton"

    If TypeOf m_buttonLocal Is CommandButton Then _
        Debug.Print "m_buttonLocal Is CommandButton"

    If TypeOf m_buttonMsForms Is MSForms.CommandButton Then _
        Debug.Print "m_buttonMsForms Is MSForms.CommandButton"

End Sub
Run Code Online (Sandbox Code Playgroud)

输出:

We have two instances of two different button types: testLocalButton and testMsButton
TypeName of both buton types returns same result
m_buttonLocal Is CommandButton
m_buttonMsForms Is MSForms.CommandButton
Run Code Online (Sandbox Code Playgroud)