简体   繁体   中英

I wish to export the vba source code from all of the Forms, Reports, and Modules in an ms-access 2013 database

Here is a sample code module. The methods I used in previous version of Access do not work because the AllModules collection no longer contains objects for Form and report modules. If you have a code module to back up, it must be done separately with the following module.

Public Sub ExportModules2013()

Dim boolCloseModule As Boolean
Dim frm As Form
Dim i As Integer
Dim iModuleType As Integer
Dim modModule As Module
Dim modOpenModules As Modules
Dim obj As AccessObject, dbs As Object
Dim rpt As Report
Dim sFileName As String
Dim sPath As String
'
sPath = "X:\Perryaire\Source\201308025\"
'
Set dbs = Application.CurrentProject
' Search for all AccessObject objects in AllForms Collection.
For Each obj In dbs.AllForms
    DoCmd.OpenForm obj.Name, acDesign
    Set frm = Forms(obj.Name)
    If frm.HasModule Then
        Set modModule = frm.Module
        GoSub L_ExportModule
    End If
DoCmd.Close acForm, frm.Name
Set frm = Nothing
Next obj
' Search for all AccessObject objects in AllReports Collection.
For Each obj In dbs.AllReports
    DoCmd.OpenReport obj.Name, acDesign
    Set rpt = Reports(obj.Name)
    If rpt.HasModule Then
        Set modModule = rpt.Module
        GoSub L_ExportModule
    End If
DoCmd.Close acReport, rpt.Name
Set rpt = Nothing
Next obj
' Search for all AccessObject objects in AllModules collection.
For Each obj In dbs.AllModules
    If Not obj.IsLoaded Then
        DoCmd.OpenModule (obj.Name)
    End If
    Set modModule = Application.Modules(obj.Name)
    GoSub L_ExportModule
    If boolCloseModule Then DoCmd.Close acModule, modModule.Name
Next obj
Exit Sub

L_ExportModule:

With modModule
    iModuleType = acStandardModule
    On Error Resume Next
    iModuleType = .Type
    sFileName = Nz(.Name, "MISSING:") & IIf(iModuleType = acStandardModule, ".bas", ".cls")

Lopen:

    On Error GoTo Lmkdir
    Open sPath & sFileName For Output As #1
    If modModule.Type = acStandardModule Then
        Print #1, "Attribute VB_Name = """ & .Name & """"
    Else
        Print #1, "VERSION 1.0 CLASS"
        Print #1, "BEGIN"
        Print #1, "   MultiUse = -1  'True"
        Print #1, "End"
        Print #1, "Attribute VB_Name = """ & .Name & """"
        Print #1, "Attribute VB_GlobalNameSpace = False"
        Print #1, "Attribute VB_Creatable = False"
        Print #1, "Attribute VB_PredeclaredId = False"
        Print #1, "Attribute VB_Exposed = False"
    End If
    Print #1, .Lines(1, CLng(.CountOfLines))

    Close #1
End With
Return

Lmkdir:

If Err.Number = "76" Then
    MkDir sPath
    Resume Lopen
Else
    MsgBox "Error: " & Err.Number & " " & Err.Description
End If

Exit Sub

End Sub

You can try the following code. It will export all the module, class and form code and save to the current project folder. Remember to refer to / Check the Microsoft Visual Basic For Applications Extensibility Library in VBE > Tools > References

Sub ExportAllCode()

  For Each c In Application.VBE.VBProjects(1).VBComponents
      Select Case c.Type
          Case vbext_ct_ClassModule, vbext_ct_Document
              Sfx = ".cls"
          Case vbext_ct_MSForm
              Sfx = ".frm"
          Case vbext_ct_StdModule
              Sfx = ".bas"    
          Case Else
              Sfx = ""
      End Select
      If Sfx <> "" Then
          c.Export _
              fileName:=CurrentProject.Path & "\" & _
              c.Name & Sfx
      End If
  Next c

  End Sub

Have you tried to use the undocumented Application.SaveAsText functions?

Here is what I use to export all forms/reports/queries/modules:

Another related question:

There is tool like this for Excel. Maybe you can look at the source and use the parts you want. http://www.codeproject.com/Articles/18029/SourceTools-xla

屏幕

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM