VERSION 1.0 CLASS BEGIN MultiUse = -1 'True Persistable = 0 'NotPersistable DataBindingBehavior = 0 'vbNone DataSourceBehavior = 0 'vbNone MTSTransactionMode = 0 'NotAnMTSObject END Attribute VB_Name = "CustomVBProject" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = False Attribute VB_Exposed = False Option Explicit ' The CustomVBProject class wraps the built-in VBProject object. ' Wrapping this object allows us not only to continue to use its ' inherent properties and methods, but also to add new custom ' properties and methods. Private p_vbpProject As VBIDE.VBProject Private p_colModules As CallTree.Modules Public Name As String Public Property Set VBProject(vbpProject As VBIDE.VBProject) ' Write-once property stores reference to VBProject object. If p_vbpProject Is Nothing Then Set p_vbpProject = vbpProject End If End Property Public Property Get VBProject() As VBIDE.VBProject ' Returns reference to the VBProject object that this ' class wraps. By using this property, you can access any ' of the built-in properties of a VBProject object ' through the CustomVBProject object. Set VBProject = p_vbpProject End Property Public Property Set Modules(colModules As CallTree.Modules) ' Object property to set Modules collection. Set p_colModules = colModules End Property Public Property Get Modules() As CallTree.Modules ' Object property to return Modules collection. Set Modules = p_colModules End Property Private Sub Class_Initialize() ' Create new Modules collection for this ' CustomVBProject object. Set Me.Modules = New Modules ' Set Parent property for Modules collection to point ' to this object. Set Me.Modules.Parent = Me End Sub Public Sub TraverseCallTree() ' Parses text of each procedure and adds references to ' called and calling procedures. Dim rstProcList As ador.Recordset Dim modModule As CallTree.Module Dim prcProc As CallTree.Procedure Dim prcCalledProc As CallTree.Procedure Dim strProcText As String Dim astrProcText() As String Dim astrMatches() As String Dim strTemp As String Dim strModuleName As String Dim strProcName As String Dim lngCount As Long ' Get recordset containing list of procedures. Set rstProcList = GetProcList(ActiveCustomVBProject) ' Loop through procedures, parsing text and comparing it against ' list of procedures for project. For Each modModule In ActiveCustomVBProject.Modules For Each prcProc In modModule.Procedures strProcText = PrepForParsing(prcProc.ProcText) ' Split procedure text into array. astrProcText = Split(strProcText) With rstProcList ' Loop through the recordset, comparing each record ' to the parsed text for this procedure. Do Until .EOF ' Store name of module and procedure from recordset. strModuleName = .Fields(MODULE_NAME) strProcName = .Fields(PROC_NAME) ' Perform filter operation to determine whether ' that procedure name appears within the text ' of the passed-in procedure (prcProc). ' Filtered array should contain only one procedure name ' (although it may contain that name in multiple ' elements). astrMatches = FilterExactMatch(astrProcText, strProcName, _ vbTextCompare) If UBound(astrMatches) >= LBound(astrMatches) Then For lngCount = LBound(astrMatches) To UBound(astrMatches) If InStr(1, astrMatches(lngCount), prcProc.Name, _ vbTextCompare) <= 0 Then ' Procedure is called from this procedure at least once. Set prcCalledProc = _ ActiveCustomVBProject.Modules(strModuleName).Procedures(strProcName) ' Add to CallsFrom collection. prcProc.CallsFrom.Add prcCalledProc ' Get reference to called procedure, and add ' reference to this procedure (prcProc) to the ' CallsTo collection of that procedure. prcCalledProc.CallsTo.Add prcProc ' It doesn't matter whether the called procedure is ' called more than once, so exit loop here. Exit For End If Next End If .MoveNext Loop ' Return to start of recordset to repeat with next procedure. .MoveFirst End With Next prcProc Next modModule End Sub Private Function RemoveCommentLines(strProcText As String) As String ' Remove comment lines from procedure text. ' This eliminates the problem of procedure names listed ' within comments. However, it handles only comments on ' separate lines; it does not handle inline comments. Dim astrLines() As String Dim lngCount As Long Dim strReturn As String astrLines = Split(strProcText, vbCrLf) For lngCount = LBound(astrLines) To UBound(astrLines) astrLines(lngCount) = TrimSpace(astrLines(lngCount)) If Left(astrLines(lngCount), 1) = "'" Then astrLines(lngCount) = "" End If Next strReturn = Join(astrLines, vbCrLf) RemoveCommentLines = strReturn End Function Private Function PrepForParsing(strProcText As String) As String ' Prepare procedure text for parsing. Dim strParse As String ' Remove comments from stored string. Note that this will ' work only for comments that are on separate lines, ' not inline comments. strParse = RemoveCommentLines(strProcText) ' Replace carriage returns, line feeds, parentheses, ' and dot operators (".") with spaces. strParse = Replace(strParse, Chr$(10), Chr$(32)) strParse = Replace(strParse, Chr$(13), Chr$(32)) strParse = Replace(strParse, "(", Chr$(32)) strParse = Replace(strParse, ")", Chr$(32)) strParse = Replace(strParse, ".", Chr$(32)) ' Trim extra spaces. strParse = TrimSpace(strParse) PrepForParsing = strParse End Function Private Function GetProcList(cvbCustom As CallTree.CustomVBProject) _ As ador.Recordset ' Returns list of all procedures in a project in an ADO ' recordset. This list is used when parsing a procedure to ' determine whether it calls another procedure in the project. ' The ADO recordset created here is used simply as a ' nonpersistent data structure. The data it contains is ' added dynamically; it's not taken from a database. Dim modModule As CallTree.Module Dim prcProc As CallTree.Procedure Dim rstProcList As ador.Recordset Dim fldField As ador.Field ' Create new recordset and append fields. Set rstProcList = New ador.Recordset ' Create variable-size text fields. rstProcList.Fields.Append MODULE_NAME, adVarChar, 255 rstProcList.Fields.Append PROC_NAME, adVarChar, 255 ' Open keyset-type cursor. rstProcList.Open CursorType:=adOpenKeyset With rstProcList For Each modModule In ActiveCustomVBProject.Modules For Each prcProc In modModule.Procedures .AddNew .Fields(MODULE_NAME) = modModule.Name .Fields(PROC_NAME) = prcProc.Name .Update Next prcProc Next modModule End With ' Move to start of recordset. If rstProcList.RecordCount > 0 Then rstProcList.MoveFirst End If ' Return recordset. Set GetProcList = rstProcList End Function