Monday, April 9, 2018

Access extract all vba modules for versionning support

Hello,

I wanted to extract all VBA modules from Microsoft ACCESS to be able to use GIT for versionning my VBA code

Here is a VBA macro to extract all module in a subfolder called:
[current directory of access file]\VBAProjectFiles

In microsoft access4
ALT F11
new module
copy paste this code in the module
run it

every file will be overwritten each time, but eh, GIT will see what line were added and deleted in each module

Required:
Access to VBA models in options, confidentiality center


-------------------- VBA module99 ---------------------------------

Option Compare Database

'====================================================
' exportation des modules VBA pour push dans GIT
'====================================================
Public Sub exportVBAlinebyline()

    path00 = CurrentDb.Name
    pathend00 = InStrRev(path00, "\")
    path01 = Left(path00, Len(path00) - (Len(path00) - pathend00))
    filename00 = Right(path00, (Len(path00) - pathend00))
    filename01 = Replace(filename00, ".", "")
   
    ' The reference for the FileSystemObject Object is Windows Script Host Object Model
    ' but it not necessary to add the reference for this procedure.
   
    Dim objfso As Object
    Dim file01 As Object
    Dim strMod As String
    Dim mdl As Object
    Dim i As Integer
   
    Set objfso = CreateObject("Scripting.FileSystemObject")
   
    ' Set up the file.
    ' SpFolder is a small function, but it would be better to fill in a
    ' path name instead of SpFolder(Desktop), eg "c:\users\somename\desktop"
   
    path02 = path01 & "VBAProjectFiles"
    If objfso.FolderExists(path02) = False Then
        err01 = 0: err02 = "": On Error Resume Next
        objfso.createfolder path02
        err01 = Err.number: err02 = Err.Description: On Error GoTo 0
    Else
        '=== folder already exist
    End If
   
    If err01 = 0 Then
       
        '=== list of all modules
        Set file01 = objfso.CreateTextFile(path01 & "\" & filename01 & "_liste.txt")
       
       
        '=== For each component in the project ...
        For Each mdl In VBE.ActiveVBProject.VBComponents
            '=== list of all modules
            file01.writeline mdl.Name
           
            Set file02 = objfso.CreateTextFile(path02 & "\" & mdl.Name & ".txt")
            '=== using the count of lines ...
            i = VBE.ActiveVBProject.VBComponents(mdl.Name).CodeModule.CountOfLines
            '=== put the code in a string ...
            If i > 0 Then
               strMod = VBE.ActiveVBProject.VBComponents(mdl.Name).CodeModule.Lines(1, i)
            End If
            '=== and then write it to a file, first marking the start with
            '=== some equal signs and the component name.
            file02.writeline strMod
            file02.Close
        Next
       
        '=== Close eveything
        file01.Close
        Set objfso = Nothing
       
    Else
        MsgBox ("ERROR cannot create folder: " & vbCrLf & path02)
    End If
   
End Sub

1 comment: