Instantly Copy All Your Code to a Text File


I could hardly believe my eyes. This was some really great code. I had come across some very good code from Remou (F. E. Boyle) before and now a web search turned up this incredible treasure.


Could it be true? Could this code really write all of my VBA code in my Database to a text file? I tweaked the code just a little to suit me and gave it a try. I could hardly believe it. In less than one second the code wrote over 14,000 lines of code to a text file. The large size was because this was one of my Code Library Databases. Every Function and Sub, even all of my Form and Report procedures were all written out formatted the same as in my database. The code even added Module headers. It just don’t get no better than this!


Perhaps you also forget where that procedure you need is located. I have spent a lot of time just looking for a snippet of code I used just the other day. Now I can easily search the text files and find that routine almost instantly. Thank you Remou!


Many times I have needed to compare the code from two or more databases. I often develop a “Working Version” and a “Sample Version” with sample data. It can be real easy to forget to add the same code to both databases.


Now I can use a number of text compare utilities to synchronize my code. If I wanted, I could even use Microsoft Word to compare and synchronize my code. How sweet is that?


I found this code on the intriguing Less than Dot site, an IT community about sharing knowledge and ideas. There is something for almost everyone. There is a wiki where all the shared knowledge is combined, blogs, forums, and articles covering a myriad of topics like Microsoft Access, .Net, SQL Server, Google, Unit testing, and Cloud Computing to name a few.


Here is the code:
.

'------------------------------------------------------------------------------------------------------
' 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 10/5/2008
' <a href="http://forum.lessthandot.com/viewtopic.php?f=95&t=379">http://forum.lessthandot.com/viewtopic.php?f=95&t=379</a>
' 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 1/1/2009
' Example 1 : Call AllCodeToTextFile("C:\Code", "txt") Save as a Text File
' Example 2 : Call AllCodeToTextFile("C:\Code", "bas") Save as a Module File
'------------------------------------------------------------------------------------------------------
'
Sub AllCodeToTextFile(strFolder As String, strFileExt As String)
    Dim fs As Object
    Dim F As Object
    Dim strMod As String
    Dim mdl As Object
    Dim i As Integer

    Set fs = 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

    ' Establish the file name using the CurrentProject Name
    strFolder = (strFolder & Replace(CurrentProject.Name, _
        ".", "") & "." & strFileExt)
    'Debug.Print strFolder

    'Set up the file.
    Set F = fs.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.
        F.WriteLine String(55, "=") & vbCrLf & mdl.Name _
            & vbCrLf & String(55, "=") & vbCrLf & strMod
    Next mdl

    MsgBox "Code has been saved to " & strFolder

    'Close eveything
    F.Close
    Set fs = Nothing
End Sub


To download the code used in this article visit our Free CodeSamples Page.


You can download the completed and free Report Date Dialog Form in the US or UK Version at our Gaining Access website.


More Free Downloads:
Pop-up Calendar
ScopeSight for Access Reports Demo: get the exact data you want on your Access Reports.
Free Church Management Software now with new Contributions management.
Code Samples


Get the Access and Outlook Appointment Manager to manage all of your Outlook Calendar Appointments and Access dated information.


Happy computing,
Patrick (Pat) Wood
Gaining Access

4 Comments

  1. Posted February 17, 2009 at 1:03 PM | Permalink

    Thanks for this, it is a sweet find indeed. I can now export all my code to one location and search it easily.

    Thanks again.

    • Posted February 17, 2009 at 2:53 PM | Permalink

      Hi Andrew,

      I am glad you find it useful. That is what I do too. It saves me a lot of time.

      Thanks to Remou for the code.

  2. Posted May 23, 2009 at 3:33 AM | Permalink

    thank you for bringing this to our attention, Pat!

    Warm Regards,
    Crystal

    Microsoft MVP

    *
    (: have an awesome day :)
    *

    • Posted May 23, 2009 at 8:55 AM | Permalink

      Hi Crystal,

      You are very welcome. I find it very helpful. It was very nice of Remou to allow me to share it with everyone.

      Kind Regards,
      Pat


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*