'---------------------------------------------------------------------------------------
' Procedure : AllCodeToTextFile
' Purpose   : Outputs all code in standard and class modules to a text file.
'           : It uses a Folder path you supply and the CurrentProject.Name
'           : to create the file name.
' Author    : Remou http://forum.lessthandot.com/viewtopic.php?f=95&t=379 10/5/2008
'           : You can output the code from all components of your project using VBE
'           : The reference for the FileSystemObject Object is Windows
'           : Script Host Object Model but it not necessary to add
'           : the reference for this procedure.
'           : Modified by Pat Wood 8/18/2011
' Example   : Call AllCodeToTextFile("E:\!0-ACCESS-PROJECTS\!0-Code\", "txt")
'---------------------------------------------------------------------------------------
'
Sub AllCodeToTextFile(strFolder As StringOptional strFileExt As String = "txt")

    Dim fso As Object
    Dim fsoFile As Object
    Dim strMod As String
    Dim mdl As Object
    Dim i As Integer
    Dim strFileName As String
    Dim strDate As String

    'Use a unique file name with the database name plus date and time
    strFileName = Replace(CurrentProject.Name, ".", "-")
    strDate = Format(Now(), "yyyy-mm-dd-hh-nn-ss")

    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Add \ to the end of the folder path if needed
    If Right$(strFolder, 1) = "\" Then
        'Do Nothing
    Else
        strFolder = strFolder & "\"
    End If

    ' Put together the file name
    strFolder = (strFolder & strFileName & "-" & strDate & "." & strFileExt)
    'Debug.Print strFolder

    'Set up the file.
    Set fsoFile = fso.CreateTextFile(strFolder)

    'For each component in the project ...
    For Each mdl In VBE.ActiveVBProject.VBComponents
        'using the count of lines ...
        i = VBE.ActiveVBProject.VBComponents(mdl.Name).CodeModule.CountOfLines
        'put the code in a string ...
        strMod = VBE.ActiveVBProject.VBComponents(mdl.Name).CodeModule.Lines(1, i)
        'and then write it to a file, first marking the start with
        'some equal signs and the component name.
        fsoFile.WriteLine String$(55, "=") & vbCrLf & mdl.Name _
                    & vbCrLf & String$(55, "=") & vbCrLf & strMod
    Next

    MsgBox "Code has been saved to " & strFolder

    'Close eveything
    fsoFile.Close
    Set fso = Nothing
End Sub