遍历用户窗体

时间:2018-07-03 08:33:45

标签: vba

我试图遍历项目组件,如果结果为3(用户窗体),则要遍历示教用户窗体并获取控件的名称和属性,但是我在第二个错误中都收到了错误。

任何帮助将不胜感激!

Option Explicit

Sub Find_From_control()

    Dim Control As Control
    Dim Component As Object
    Dim LastRow As Long

     For Each Component In ThisWorkbook.VBProject.VBComponents
        If Component.Type = 3 Then

            For Each Control In Form.Controls
                LastRow = wsControl.Range("I" & Rows.Count).End(xlUp).Row

                If TypeName(Control) = "TabStrip" Or TypeName(Control) = "ScrollBar" Or TypeName(Control) = "SpinButton" Or TypeName(Control) = "MultiPage" Or TypeName(Control) = "TextBox" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = Control.Type
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                ElseIf TypeName(Control) = "Image" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = Control.Type
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                ElseIf TypeName(Control) = "Frame" Or TypeName(Control) = "ToggleButton" Or TypeName(Control) = "OptionButton" Or TypeName(Control) = "CheckBox" Or TypeName(Control) = "Label" Or TypeName(Control) = "CommandButton" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = Control.Type
                    wsControl.Range("K" & LastRow + 1).Value = Control.Caption
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                ElseIf TypeName(Control) = "ListBox" Or TypeName(Control) = "ComboBox" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = Control.Type
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                    wsControl.Range("N" & LastRow + 1).Value = Control.ColumnCount
                End If

            Next
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

用户表单是组件Component.Designer

Control.Type不是有效的属性,请改用TypeName(Control)

Sub Find_From_control()

    Dim Control As Control
    Dim Component As Object
    Dim Form As UserForm
    Dim LastRow As Long

     For Each Component In ThisWorkbook.VBProject.VBComponents
        If Component.Type = 3 Then
            Set Form = Component.Designer
            For Each Control In Form.Controls
                LastRow = wsControl.Range("I" & Rows.count).End(xlUp).Row

                If TypeName(Control) = "TabStrip" Or TypeName(Control) = "ScrollBar" Or TypeName(Control) = "SpinButton" Or TypeName(Control) = "MultiPage" Or TypeName(Control) = "TextBox" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = TypeName(Control)
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                ElseIf TypeName(Control) = "Image" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = TypeName(Control)
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                ElseIf TypeName(Control) = "Frame" Or TypeName(Control) = "ToggleButton" Or TypeName(Control) = "OptionButton" Or TypeName(Control) = "CheckBox" Or TypeName(Control) = "Label" Or TypeName(Control) = "CommandButton" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = TypeName(Control)
                    wsControl.Range("K" & LastRow + 1).Value = Control.Caption
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                ElseIf TypeName(Control) = "ListBox" Or TypeName(Control) = "ComboBox" Then
                    wsControl.Range("I" & LastRow + 1).Value = Control.Name
                    wsControl.Range("J" & LastRow + 1).Value = TypeName(Control)
                    wsControl.Range("L" & LastRow + 1).Value = Control.Tag
                    wsControl.Range("M" & LastRow + 1).Value = Control.TabIndex
                    wsControl.Range("N" & LastRow + 1).Value = Control.ColumnCount
                End If

            Next
        End If
    Next
End Sub