MZ-Tools has a utility that does this, along with a host of other functions. It's a commercial tool. I consider it well worth the cost of a license.Can someone help with some code that can make a list of empty vba functions and procedures in my access application? I need to do some code cleaning up. e.g.
Code:Private Sub Form_Load() End Sub
I was trying to see if MZ-Tools could do this, but could not find that feature. Where is that feature?has a utility that does this
In Review Quality feature - Select Dead Code ReviewWhere is that feature?
Thanks!In Review Quality feature - Select Dead Code Review
Sub EmptyDummy()
End Sub
Option Compare Database
Option Explicit
'http://www.team-moeller.de/?Tipps_und_Tricks:Rund_um_die_VB-IDE:Pruefen_ob_Modul_leer_ist
'---------------------------------------------------------------------------------------
' Procedure : ModulesEmptyList
' Author : http://www.team-moeller.de/?Tipps_und_Tricks:Rund_um_die_VB-IDE:Pruefen_ob_Modul_leer_ist
' : Modified by Patrick Wood - Gaining Access Technologies http://gainingaccess.net/
' Date : 1/24/2012
' Purpose : Produce a list of empty modules
'---------------------------------------------------------------------------------------
'
Public Function ModulesEmptyList() As String
On Error GoTo ErrHandle
Dim vbcModule As Object
Dim lngLine As Long
Dim strLine As String
Dim strList As String
Dim blnEmpty As Boolean
ModulesEmptyList = False 'Default Value
With VBE.ActiveVBProject
'For each component in the project ...
For Each vbcModule In .VBComponents
blnEmpty = False 'Assuming the Module is Not Empty.
'If the Module Declaration lines = total count of lines in the Module check it.
If .VBComponents(vbcModule.Name).CodeModule.CountOfLines _
= .VBComponents(vbcModule.Name).CodeModule.CountOfDeclarationLines Then
blnEmpty = True 'Assuming the Module is Empty.
For lngLine = 1 To .VBComponents(vbcModule.Name).CodeModule.CountOfLines
strLine = Trim$(.VBComponents(vbcModule.Name).CodeModule.Lines(lngLine, 1))
If Len(strLine) > 0 Then
If Left$(strLine, 6) <> "Option" Then
If Left$(strLine, 1) <> "'" Then
If Left$(strLine, 3) <> "Rem" Then
blnEmpty = False
Exit For
End If
End If
End If
End If
Next lngLine
'Add any Empty Module Names to list.
If blnEmpty = True Then
strList = strList & vbcModule.Name & vbCrLf
End If
End If
Next vbcModule
End With
'Build the Return string
If Len(strList & "") > 0 Then
strList = "********** List of Empty Modules **********" & vbCrLf & strList
Else
strList = "No empty Modules were found."
End If
ModulesEmptyList = strList
Debug.Print strList
ExitHere:
On Error Resume Next
Set vbcModule = Nothing
Exit Function
ErrHandle:
Call MsgBox(Err.Number & " " & Err.Description _
& " In Procedure ModulesEmptyList")
Resume ExitHere
End Function
Public Function GetEmptyProcedures() As String
On Error GoTo Err_Handler
Dim VBAEditor As VBIDE.VBE
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim lineNum As Long
Dim NumLines As Long
Dim ProcName As String
Dim ProcKind As VBIDE.vbext_ProcKind
Dim PI As ProcInfo
Dim strOut As String
Dim ProcStart As Boolean
Dim CurrentProc As String
Dim OldProc As String
Set VBAEditor = Application.VBE
Set VBProj = VBAEditor.ActiveVBProject
For Each VBComp In VBProj.VBComponents
If VBComp.Name <> "modVBECode" Then
Set CodeMod = VBComp.CodeModule
With CodeMod
lineNum = .CountOfDeclarationLines + 1
CurrentProc = .ProcOfLine(lineNum, ProcKind)
Do Until lineNum >= .CountOfLines
ProcName = .ProcOfLine(lineNum, ProcKind)
'Debug.Print ProcName
lineNum = .ProcStartLine(ProcName, ProcKind) + _
.ProcCountLines(ProcName, ProcKind) + 1
If Not HasCode(CodeMod, ProcName) Then
If strOut = "" Then
strOut = "Module: " & VBComp.Name & " Procedure: " & ProcName
Else
strOut = strOut & vbCrLf & "Module: " & VBComp.Name & " Procedure: " & ProcName
End If
End If
Loop
End With
End If
Debug.Print strOut
Next VBComp
GetEmptyProcedures = strOut
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & " in GetEmptyProcedures : " & Err.Description
GoTo Exit_Handler
End Function
Public Function HasCode(CodeMod As CodeModule, ProcName As String) As Boolean
Dim lineNum As Long
Dim Thestart As Long
Dim Thestop As Long
Dim theLine As String
Dim ProcKind As VBIDE.vbext_ProcKind
Thestart = CodeMod.ProcStartLine(ProcName, ProcKind)
Thestop = Thestart + CodeMod.ProcCountLines(ProcName, ProcKind)
' Debug.Print Thestart & vbCrLf & Thestop
For lineNum = Thestart To Thestop
theLine = Trim(CodeMod.Lines(lineNum, 1))
If Len(theLine) > 0 Then
If Left$(theLine, 1) <> "'" And Left$(theLine, 3) <> "Rem" And Left(theLine, 8) <> "Private " And Left(theLine, 7) <> "Public " And Left(theLine, 4) <> "End " Then
HasCode = True
' Debug.Print theLine
Exit Function
End If
End If
Next lineNum
End Function
Thestart = CodeMod.ProcStartLine(ProcName, ProcKind)
Thestop = Thestart + CodeMod.ProcCountLines(ProcName, ProcKind)
The official documentation is here: Visual Basic Add-in Model referenceDoes anyone have a reference to the VBE Object model that shows all the pieces and their relationship to each other?