Exporting MS Access Forms and Class / Modules Recursively to text files?

后端 未结 6 1228
时光取名叫无心
时光取名叫无心 2020-12-01 05:57

I found some code on an ancient message board that nicely exports all of the VBA code from classes, modules and forms (see below):

Option Explicit
Option Com         


        
6条回答
  •  心在旅途
    2020-12-01 06:27

    Like for MS Excel, you can also use a loop over the Application.VBE.VBProjects(1).VBComponents and use the Export method to export your modules/classes/forms:

    Const VB_MODULE = 1
    Const VB_CLASS = 2
    Const VB_FORM = 100
    Const EXT_MODULE = ".bas"
    Const EXT_CLASS = ".cls"
    Const EXT_FORM = ".frm"
    Const CODE_FLD = "Code"
    
    Sub ExportAllCode()
    
    Dim fileName As String
    Dim exportPath As String
    Dim ext As String
    Dim FSO As Object
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    ' Set export path and ensure its existence
    exportPath = CurrentProject.path & "\" & CODE_FLD
    If Not FSO.FolderExists(exportPath) Then
        MkDir exportPath
    End If
    
    ' The loop over all modules/classes/forms
    For Each c In Application.VBE.VBProjects(1).VBComponents
        ' Get the filename extension from type
        ext = vbExtFromType(c.Type)
        If ext <> "" Then
            fileName = c.name & ext
            debugPrint "Exporting " & c.name & " to file " & fileName
            ' THE export
            c.Export exportPath & "\" & fileName
        Else
            debugPrint "Unknown VBComponent type: " & c.Type
        End If
    Next c
    
    End Sub
    
    ' Helper function that translates VBComponent types into file extensions
    ' Returns an empty string for unknown types
    Function vbExtFromType(ByVal ctype As Integer) As String
        Select Case ctype
            Case VB_MODULE
                vbExtFromType = EXT_MODULE
            Case VB_CLASS
                vbExtFromType = EXT_CLASS
            Case VB_FORM
                vbExtFromType = EXT_FORM
        End Select
    End Function
    

    Only takes a fraction of a second to execute.

    Cheers

提交回复
热议问题