VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Modules"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

' Private collection to hold Module objects.
Private p_colModules As Collection

' Private variable stores reference to parent object.
Private p_cvbCustomVBProject As ExportToCodeLib.CustomVBProject

Function Add(strModuleName As String, _
    lngType As vbext_ComponentType, _
    codCodeModule As VBIDE.CodeModule) As ExportToCodeLib.Module
    ' Add module to Modules collection and return Module object.
    ' Set Name, ModuleType, and CodeModule properties.

    Dim modModule As ExportToCodeLib.Module
    
    ' Create new Module object.
    Set modModule = New Module
    
    ' Set properties for module.
    With modModule
        .Name = strModuleName
        .ModuleType = lngType
        Set .CodeModule = codCodeModule
        ' Return text of module.
        .FullText = .CodeModule.Lines(1, .CodeModule.CountOfLines)
        ' Set Module object's parent to CustomVBProject object.
        ' The Modules collection and Module object both have
        ' a CustomVBProject object as their parent.
        Set .Parent = Me.Parent
        ' Add module's procedures to its Procedures collection.
        AddProcsToCollection modModule
    End With
    
    ' Add to private collection.
    p_colModules.Add modModule, strModuleName
    
    ' Return reference to new Module object.
    Set Add = modModule
End Function

Property Get Count() As Long
    Count = p_colModules.Count
End Property

Property Get Item(varIndex As Variant) As Module
Attribute Item.VB_UserMemId = 0
    Set Item = p_colModules.Item(varIndex)
End Property

Private Sub Class_Initialize()
    ' Create new collection to hold Modules collection.
    Set p_colModules = New Collection
    
End Sub

Public Property Set Parent(cvbCustomVBProject As ExportToCodeLib.CustomVBProject)
    ' Set Parent property to refer to CustomVBProject object.
    If p_cvbCustomVBProject Is Nothing Then
        Set p_cvbCustomVBProject = cvbCustomVBProject
    End If
End Property

Public Property Get Parent() As ExportToCodeLib.CustomVBProject
    ' Retrieve object reference stored in Parent property.
    Set Parent = p_cvbCustomVBProject
End Property

Private Function AddProcsToCollection(modModule As ExportToCodeLib.Module)
    ' Parse module in order to add procedures to module's
    ' Procedures collection.
    Dim vbpProject      As VBIDE.VBProject
    Dim vbcComponent    As VBIDE.VBComponent
    Dim prcProc         As ExportToCodeLib.Procedure
    Dim lngLines        As Long
    Dim lngDeclLines    As Long
    Dim lngCount        As Long
    Dim lngLoop         As Long
    Dim lngProcType     As Long
    Dim strProcName     As String
    
    ' Return reference to parent project.
    Set vbpProject = modModule.Parent.VBProject
    
    ' Add module's procedures to collection.
    With modModule.CodeModule
        ' Count lines in module.
        lngLines = .CountOfLines
        ' Count lines in Declarations section in module.
        lngDeclLines = .CountOfDeclarationLines
        ' Determine name and type of first procedure.
        strProcName = .ProcOfLine(lngDeclLines + 1, lngProcType)
        ' Make sure module contains procedures.
        If Len(strProcName) <> 0 Then
            ' Create new Procedure object.
            Set prcProc = modModule.Procedures.Add(strProcName)
            ' Set properties for new Procedure object.
            With prcProc
                .Name = strProcName
                .ModuleName = modModule.Name
                .ProcType = lngProcType
                .ProcText = GetProcText(modModule.CodeModule, _
                    strProcName, lngProcType)
                .CommentText = GetCommentText(modModule.CodeModule, _
                    strProcName, lngProcType)
             End With
        
            ' After retrieving first procedure, retrieve
            ' others in the module.
            ' Initialize counter variable.
            lngCount = 0
            ' Determine procedure name for each line after
            ' Declarations section.
            For lngLoop = lngDeclLines + 1 To lngLines
            ' Compare procedure name with ProcOfLine
            ' property value to determine whether we've
            ' reached new procedure yet.
                If strProcName <> .ProcOfLine(lngLoop, lngProcType) Then
                    ' Increment counter.
                    lngCount = lngCount + 1
                    ' Store name of next procedure.
                    strProcName = .ProcOfLine(lngLoop, lngProcType)
                    ' Add new Procedure object for that
                    ' procedure.
                    Set prcProc = modModule.Procedures.Add(strProcName)
                    ' Set properties for new Procedure object.
                    With prcProc
                        .Name = strProcName
                        .ModuleName = modModule.Name
                        .ProcType = lngProcType
                        .ProcText = GetProcText(modModule.CodeModule, _
                            strProcName, lngProcType)
                        .CommentText = GetCommentText(modModule.CodeModule, _
                            strProcName, lngProcType)
                    End With
                End If
            Next lngLoop
        End If
    End With
End Function

Private Function GetProcText(cmdModule As VBIDE.CodeModule, _
                             strProcName As String, _
                             lngProcType As Long) As String
    ' Returns body of procedure.
    
    Dim lngStartLine    As Long
    Dim lngBodyLine     As Long
    Dim lngEndProc      As Long
    Dim lngCount        As Long

    With cmdModule
        ' Count lines in procedure.
        lngCount = .ProcCountLines(strProcName, lngProcType)
        ' Determine start line.
        lngStartLine = .ProcStartLine(strProcName, lngProcType)
        ' Determine body line.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Determine line number of last line in procedure.
        lngEndProc = (lngBodyLine + lngCount - 1) - Abs(lngBodyLine - lngStartLine)
        ' Return body of procedure.
        GetProcText = .Lines(lngBodyLine, (lngEndProc - lngBodyLine) + 1)
    End With
End Function

Private Function GetHeaderText(cmdModule As VBIDE.CodeModule, _
                               strProcName As String, _
                               lngProcType As Long) As String
    ' Returns header text for procedure.
    
    Dim lngStartLine    As Long
    Dim lngBodyLine     As Long
    Dim lngEndProc      As Long
    Dim lngCount        As Long

    With cmdModule
        ' Count lines in procedure.
        lngCount = .ProcCountLines(strProcName, lngProcType)
        ' Determine start line.
        lngStartLine = .ProcStartLine(strProcName, lngProcType)
        ' Determine body line.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Determine line number of last line in procedure.
        lngEndProc = (lngBodyLine + lngCount - 1) - Abs(lngBodyLine - lngStartLine)
        ' Return procedure header.
        If lngBodyLine - lngStartLine > 0 Then
            GetHeaderText = Trim$(.Lines(lngStartLine, (lngBodyLine - lngStartLine)))
        Else
            GetHeaderText = ""
        End If
    End With
End Function

Private Function GetCommentText(cmdModule As VBIDE.CodeModule, _
                                strProcName As String, _
                                lngProcType As Long) As String
    ' Returns text of comment at beginning of procedure. This is not
    ' failproof, of course, but it's about the best that can be done.

    Dim lngBodyLine             As Long
    Dim lngEndOfProc            As Long
    Dim lngCountExtBodyLines    As Long
    Dim lngEndLine              As Long
    Dim lngEndCol               As Long
    Dim strLine                 As String
    Dim strComment              As String
    Dim blnFind                 As Boolean

    With cmdModule
        ' Get number of body line for this procedure.
        lngBodyLine = .ProcBodyLine(strProcName, lngProcType)
        ' Get number of last line in procedure.
        lngEndOfProc = .ProcCountLines(strProcName, lngProcType) _
            + lngBodyLine
        ' Specify end of procedure as ending line for search.
        lngEndLine = lngEndOfProc
        
        ' Find first comment, starting with line after body line.
        blnFind = .Find("'", lngBodyLine + 1, 0, _
            lngEndLine, lngEndCol)
        ' If comment was found, check whether subsequent lines
        ' also include comments.
        If blnFind Then
            strLine = .Lines(lngEndLine, 1)
            If InStr(strLine, "'") = 1 Then
            ElseIf Asc(Mid$(strLine, InStr(strLine, "'") - 1, 1)) <> 32 Then
                Exit Function
            End If
            ' Store first line.
            strComment = Trim$(.Lines(lngEndLine, 1))
            Do
                ' Increment line number.
                lngEndLine = lngEndLine + 1
                ' Trim space from left of line.
                strLine = Trim$(.Lines(lngEndLine, 1))
                ' Check whether first character is
                ' an apostrophe ("'").
                If Left(strLine, 1) = "'" Then
                    ' Store line containing comment.
                    strComment = strComment & vbCrLf & _
                        Trim$(.Lines(lngEndLine, 1))
                Else
                    ' If line is not a comment, exit loop.
                    Exit Do
                End If
            Loop
            ' Replace apostrophes and carriage return/line feed.
            strComment = Replace(strComment, "'", " ")
            strComment = Replace(strComment, vbCrLf, " ")
            ' Trim white space from string.
            strComment = TrimSpace(strComment)
        Else
            strComment = ""
        End If
    End With
    GetCommentText = strComment
End Function

' NewEnum must return the IUnknown interface of a
' collection's enumerator.
Public Function NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    ' NewEnum function enables For Each...Next for
    ' enumerating this collection.
   Set NewEnum = p_colModules.[_NewEnum]
End Function
