Thanks to the examples on Chip Pearson's website I could make a quite useful
sub that list the different VBProject components.
In case somebody might find this useful, this is the code:
Function CompTypeToName(VBComp As VBComponent) As String
'taken from:
http://www.cpearson.com/excel/vbe.htm
'-------------------------------------------------
Select Case VBComp.Type
Case vbext_ct_ActiveXDesigner
CompTypeToName = "ActiveX Designer"
Case vbext_ct_ClassModule
CompTypeToName = "Class Module"
Case vbext_ct_Document
CompTypeToName = "Document"
Case vbext_ct_MSForm
CompTypeToName = "MS Form"
Case vbext_ct_StdModule
CompTypeToName = "Standard Module"
Case Else
End Select
End Function
Sub ListModules()
Dim i As Byte
Dim VBProj As VBProject
Dim VBComp As VBComponent
Dim compCount As Integer
Dim wb As Workbook
Dim ai As AddIn
Dim compArray()
For Each wb In Application.Workbooks
Select Case MsgBox("LIST THE VB COMPONENTS OF THIS WORKBOOK?"
& _
vbCrLf & vbCrLf & _
wb.Name, _
vbYesNoCancel + vbQuestion + vbDefaultButton1, _
"LIST VB COMPONENTS")
Case vbYes
Set VBProj = wb.VBProject
Exit For
Case vbCancel
Exit Sub
End Select
Next
If VBProj Is Nothing Then
For Each ai In Application.AddIns
If ai.Installed = True Then
Select Case MsgBox("LIST THE VB COMPONENTS OF THIS
ADD-IN?" & _
vbCrLf & vbCrLf & _
ai.Name, _
vbYesNoCancel + vbQuestion +
vbDefaultButton2, _
"LIST VB COMPONENTS")
Case vbYes
Set VBProj = Application.Workbooks(ai.Name).VBProject
Exit For
Case vbCancel
Exit Sub
End Select
End If
Next
End If
If VBProj Is Nothing Then
Exit Sub
End If
compCount = VBProj.VBComponents.Count
ReDim compArray(1 To compCount + 1, 1 To 4)
compArray(1, 1) = "Component Type"
compArray(1, 2) = "Component Name"
compArray(1, 3) = "Count Of Lines"
compArray(1, 4) = "Count Of Declaration Lines"
i = 1
On Error Resume Next 'for in case you can't do .CountOfLines
For Each VBComp In VBProj.VBComponents
i = i + 1
compArray(i, 1) = CompTypeToName(VBComp)
compArray(i, 2) = VBComp.Name
compArray(i, 3) = VBComp.CodeModule.CountOfLines
compArray(i, 4) = VBComp.CodeModule.CountOfDeclarationLines
Next
Application.ScreenUpdating = False
Cells.Clear
Range(Cells(1), Cells(i, 4)) = compArray
'sort by component type then by CountOfLines both ascending
Range(Cells(1), Cells(i, 4)).Sort Key1:=Cells(1), _
Order1:=xlAscending, _
Key2:=Cells(3), _
Order2:=xlAscending, _
Header:=xlYes
With Range(Cells(1), Cells(4))
.Font.Bold = True
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
End With
Range(Cells(1), Cells(i, 4)).Columns.AutoFit
Application.ScreenUpdating = True
End Sub
RBS