Currently Rubberduck VBA files all the VBProject VBComponents in that do not have a Folder Annotation in a Folder named after the VBProject. It can be time consuming to manually organize the components for the first time and a inconvenient to add annotations to every Worksheet, Userform, Class, ...ect. When ran my code will organize any unfiled component by ProjectName.ComponentType
.
Before
After
You will need to add a reference to the Microsoft Visual Basic for Applications Extensibility 5.3
and from file options check "Trust access to VBA project object model".
Private Sub AddDefaultFolderAnnotationsToVBComponents()
Dim Component As VBComponent
Dim Project As VBProject
Select Case Application.Name
Case "Microsoft Access", "Microsoft Word"
Set Project = IIf(True, Application, 0).VBE.ActiveVBProject
Case "Microsoft Excel"
Set Project = IIf(True, Application, 0).ActiveWorkbook.VBProject
Case "Microsoft PowerPoint"
Set Project = IIf(True, Application, 0).VBE.VBProjects(1)
End Select
Dim FolderName As String
Dim HasFolder As Boolean
For Each Component In Project.VBComponents
With Component.CodeModule
If .CountOfLines = 0 Then
HasFolder = False
Else
HasFolder = InStr(.Lines(1, .CountOfLines), Chr(39) & "@Folder")
End If
End With
If Not HasFolder Then
Select Case Component.Type
Case vbext_ComponentType.vbext_ct_StdModule
FolderName = Project.Name & ".Modules"
Case vbext_ComponentType.vbext_ct_ClassModule
FolderName = Project.Name & ".Classes"
Case vbext_ComponentType.vbext_ct_MSForm
FolderName = Project.Name & ".Forms"
Case vbext_ComponentType.vbext_ct_ActiveXDesigner
FolderName = Project.Name & ".Designers"
Case vbext_ComponentType.vbext_ct_Document
FolderName = Project.Name & ".Documents"
End Select
Component.CodeModule.InsertLines 1, Chr(39) & "@Folder(""" & FolderName & """)"
End If
Next
End Sub
Note: I purposely kept all the functionality in a single procedure for portability.
Kudos to the Rubberduck team the latest version Rubberduck VBA v2.5 is a C# WPF that follows the MVVM pattern to the letter!