2017-10-23 4 views
1

작업엑셀 VBA - 주어진 통합 문서

내 목표에 대한 모든 정의 폼 목록 컨트롤은 특정 통합 문서에 대한 모든 정의 폼의 모든 컨트롤을 표시하는 것입니다. 내 코드는 통합 문서 모음 다른 내 모든 통합 문서에서 호출 통합 문서 (ThisWorkBook)보다 작동합니다.

문제

나는 전화 통합 문서에 대한 모든 정의 폼 '컨트롤을 표시하려고하면, 나는 오류 91 개체 변수를 얻거나 블록 변수 소위 번호가 오류 라인 200 (에를 설정하지 ERL). 아래의 코드는 실수로 오류를 표시하기 위해 2 개의 중복 부분 으로 열거되어 있습니다. 어떤 도움을 주셔서 감사합니다.

양식이 표시되면, 당신은 그것의 디자이너에 프로그래밍 방식으로 액세스를 얻을 수없는 코드

Sub ListWBControls() 
' Purpose: list ALL userform controls of a given workbook within workbooks collection 
' 
Dim bProblem As Boolean 
Dim vbc  As VBIDE.VBComponent   ' module, Reference to MS VBA Exte 5.3 needed !!! 
Dim ctrl  As MSForms.Control 
Dim i  As Integer, imax As Integer ' control counters 
Dim cnr  As Long, vbcnr As Long 
Dim sLit  As String 
Dim sMsg  As String      ' result string 
Dim owb  As Workbook     ' workbook object 
Dim wb  As String      ' workbook name to choose by user 
' -------------------- 
' choose Workbook name 
' -------------------- 
    wb = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 0) ' << existing workbook name chosen in combobox 
' check if wb is calling workbook or other 
     For Each owb In Workbooks 
      If owb.Name = wb And ThisWorkbook.Name = wb Then 
      bProblem = True 
      Exit For 
      End If 
     Next owb 
' count workbooks 
    imax = Workbooks.Count 
    i = 1 
' a) start message string showing workbook name 
    sMsg = sMsg & vbNewLine & String(25, "=") & vbNewLine & _ 
      sLit & " WorkBook: " & Workbooks(i).Name & vbNewLine & String(25, "=") 
'------------------------------ 
'Loop thru components (modules) - if of UserForm type 
'------------------------------ 
For Each vbc In Workbooks(wb).VBProject.VBComponents 
    ' Only if Component type is UserForm 
    If vbc.Type = vbext_ct_MSForm Then 
    ' increment component and ctrl counters 
     sLit = Chr(i + 64) & "." 
     vbcnr = vbcnr + 1000 
     cnr = vbcnr 

    ' b) build message new component 
     sMsg = sMsg & vbNewLine & String(25, "-") & vbNewLine & sLit & cnr & " '" & _ 
       vbc.Name & "'" & vbNewLine & String(25, "-") 
    '------------------- 
    ' Loop thru controls 
    '------------------- 
    ' =================================================================== 
    ' Code is intently broken into 2 portions, to show error explicitly ! 
    ' =================================================================== 
     On Error GoTo OOPS ' Error handler --> Error 91: Object variable or With block variable not set 

     If Not bProblem Then ' part 1 - other workbooks: shown explicitly, are no problem 
100   For Each ctrl In Workbooks(wb).VBProject.VBComponents(vbc.Name).Designer.Controls 
      ' increment ctrl counter 
       cnr = cnr + 1 
      ' c) build messages controls) 
       sMsg = sMsg & vbLf & " " & Format(cnr, "0 000") & " " & ctrlInfo(ctrl) 
      Next 
     Else     ' part 2 - problem arises here (wb = calling workbook) 
200   For Each ctrl In Workbooks(wb).VBProject.VBComponents(vbc.Name).Designer.Controls ' << ERROR 91 
      ' increment ctrl counter 
       cnr = cnr + 1 
      ' c) build messages controls) 
       sMsg = sMsg & vbLf & " " & Format(cnr, "0 000") & " " & ctrlInfo(ctrl) 
      Next 

     End If 

     i = i + 1  ' increment letter counter i 
    End If 
Next vbc 
' show result 
Debug.Print sMsg 
Exit Sub 

OOPS: 
MsgBox "Error No " & Err.Number & " " & Err.Description & vbNewLine & _ 
     "Error Line " & Erl 
End Sub 

도우미 기능

Private Function ctrlInfo(ctrl As MSForms.Control) As String 
' Purpose: helper function returning userform control information 
    ctrlInfo = Left(TypeName(ctrl) & String(5, " "), 5) & " " & _ 
      Left(ctrl.Name & String(20, " "), 20) & vbTab & _ 
      " .." & IIf(TypeName(ctrl.Parent) = "UserForm", "Me " & String(15, " "), _ 
         TypeName(ctrl.Parent) & ": " & _ 
          Left(ctrl.Parent.Caption & String(15, " "), 15)) & vbTab & _ 
      " T " & Format(ctrl.Top, "# 000") & "/ L " & Format(ctrl.Left, "# 000") 
End Function 

답변