title: I wish to export the vba source code from all of the Forms, Reports, and Modules in an ms-access 2013 database
body: <p>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. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>L_ExportModule:</p>
  
  <pre><code>With modModule
      iModuleType = acStandardModule
      On Error Resume Next
      iModuleType = .Type
      sFileName = Nz(.Name, "MISSING:") &amp; IIf(iModuleType = acStandardModule, ".bas", ".cls")
  </code></pre>
  
  <p>Lopen:</p>
  
  <pre><code>    On Error GoTo Lmkdir
      Open sPath &amp; sFileName For Output As #1
      If modModule.Type = acStandardModule Then
          Print #1, "Attribute VB_Name = """ &amp; .Name &amp; """"
      Else
          Print #1, "VERSION 1.0 CLASS"
          Print #1, "BEGIN"
          Print #1, "   MultiUse = -1  'True"
          Print #1, "End"
          Print #1, "Attribute VB_Name = """ &amp; .Name &amp; """"
          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
  </code></pre>
  
  <p>Lmkdir:</p>
  
  <pre><code>If Err.Number = "76" Then
      MkDir sPath
      Resume Lopen
  Else
      MsgBox "Error: " &amp; Err.Number &amp; " " &amp; Err.Description
  End If
  
  Exit Sub
  </code></pre>
  
  <p>End Sub</p>