Option Explicit
'REFERENCE SET:
'Microsoft Visual Basic for Applications Extensibility 5.3
Private Sub cmbCancel_Click()
Unload Me
End Sub
Private Sub cmbOK_Click()
Dim i As Long
With Me.lbModules
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Call ListAllVariables(.List(i, 0), .List(i, 1))
Exit For
End If
Next i
End With
Unload Me
End Sub
Private Sub lbModules_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Call cmbOK_Click
End Sub
Private Sub UserForm_Initialize()
Dim VBproj As VBIDE.VBProject
Dim VBmod As VBIDE.VBComponent
Dim iLine As Long, aVals() As Variant
Dim sName As String
iLine = 1
On Error Resume Next
For Each VBproj In Application.VBE.VBProjects
If Len(VBproj.Filename) = 0 Then GoTo SkipProj
If VBproj.Protection <> vbext_pp_locked Then
For Each VBmod In VBproj.VBComponents
ReDim Preserve aVals(1 To 3, 1 To iLine)
sName = Right(VBproj.Filename, Len(VBproj.Filename) - _
InStrRev(VBproj.Filename, Application.PathSeparator))
aVals(1, iLine) = sName
aVals(2, iLine) = VBmod.Name
aVals(3, iLine) = ComponentTypeToString(VBmod.Type)
iLine = iLine + 1
Next VBmod
End If
SkipProj:
Next VBproj
If iLine > 1 Then
Me.lbModules.List = Application.Transpose(aVals())
End If
End Sub
Private Sub ListAllVariables(sProjName As String, sModName As String)
Dim vbeProj As VBIDE.VBProject, vbeComp As VBIDE.VBComponent
Dim VBmod As VBIDE.CodeModule
Dim WB As Workbook, WS As Worksheet
Dim iLine As Long, sLine As String
Dim iCnt As Long, iRow As Long, i As Long
Dim aVars() As String, bLocked As Boolean
Dim bNextLine As Boolean, bAssumed As Boolean
On Error Resume Next
Set WB = Workbooks.Add(xlWBATWorksheet)
Set WS = WB.Sheets(1)
iLine = 1
iRow = 4
bLocked = False
bNextLine = False
bAssumed = False
Set vbeProj = Application.Workbooks(sProjName).VBProject
If vbeProj.Protection = vbext_pp_locked Then
bLocked = True
GoTo SkipModule
End If
Set vbeComp = vbeProj.VBComponents(sModName)
Set VBmod = vbeComp.CodeModule
For iLine = 1 To VBmod.CountOfLines
sLine = Trim(VBmod.Lines(iLine, 1))
If InStr(1, sLine, "Dim", vbTextCompare) = 0 Then
If bNextLine = False Then GoTo SkipLine
End If
If InStr(1, sLine, "Dim""", vbTextCompare) <> 0 Then GoTo SkipLine
If InStr(1, sLine, "ReDim", vbTextCompare) <> 0 Then GoTo SkipLine
If Left(Trim(sLine), 1) = "'" Then GoTo SkipLine
If InStr(1, sLine, ": ", vbTextCompare) <> 0 Then sLine = Left(sLine, InStr(1, sLine, ": ", vbTextCompare) - 1)
If bNextLine = True Then
bNextLine = False
Else
sLine = Right(sLine, Len(sLine) - 4)
End If
iCnt = Len(sLine) - Len(Replace(sLine, ",", ""))
Erase aVars()
ReDim aVars(iCnt) As String
aVars = Split(sLine, ", ")
For i = LBound(aVars) To UBound(aVars)
If Trim(aVars(i)) = "_" Then
bNextLine = True
GoTo SkipVar
End If
If InStr(1, aVars(i), " As ", vbTextCompare) <> 0 Then
WS.Cells(iRow, 1).Value = Trim(Split(aVars(i), " As ")(0))
WS.Cells(iRow, 2).Value = Trim(Split(aVars(i), " As ")(1))
Else
WS.Cells(iRow, 1).Value = Trim(aVars(i))
WS.Cells(iRow, 2).Value = "Variant*"
bAssumed = True
End If
iRow = iRow + 1
SkipVar:
Next i
SkipLine:
Next iLine
SkipModule:
If iRow = 4 Then
WB.Close False
If bLocked = True Then
MsgBox "The project is locked.", vbInformation
Else
MsgBox "No variables were found.", vbInformation
End If
Exit Sub
End If
WS.Cells(1, 1).Value = sProjName
WS.Cells(1, 2).Value = sModName
WS.Cells(3, 1).Value = "VARIABLE"
WS.Cells(3, 2).Value = "TYPE"
If bAssumed = True Then WS.Cells(3, 3).Value = "* assumed type"
WS.Cells.EntireColumn.AutoFit
End Sub
Function ComponentTypeToString(ComponentType As VBIDE.vbext_ComponentType) As String
Select Case ComponentType
Case vbext_ct_ActiveXDesigner
ComponentTypeToString = "ActiveX Designer"
Case vbext_ct_ClassModule
ComponentTypeToString = "Class Module"
Case vbext_ct_Document
ComponentTypeToString = "Document Module"
Case vbext_ct_MSForm
ComponentTypeToString = "UserForm"
Case vbext_ct_StdModule
ComponentTypeToString = "Code Module"
Case Else
ComponentTypeToString = "Unknown Type: " & CStr(ComponentType)
End Select
End Function