Exporting the whole vba code from an ms access application

12,206

Try this:

Public Sub ExportVBAComponents()

  Dim wbPath As String
  Dim vbComp As Object
  Dim exportPath As String

  wbPath = ActiveWorkbook.Path

  For Each vbComp In ActiveWorkbook.VBProject.VBComponents
    exportPath = wbPath & "\" & vbComp.Name & Format$(Now, "_yyyymmdd_hhnnss")

    Select Case vbComp.Type
        Case 1 ' Standard Module
            exportPath = exportPath & ".bas"
        Case 2 ' UserForm
            exportPath = exportPath & ".frm"
        Case 3 ' Class Module
            exportPath = exportPath & ".cls"
        Case Else ' Anything else
            exportPath = exportPath & ".bas"
    End Select

    On Error Resume Next
    vbComp.Export exportPath
    On Error GoTo 0
  Next

End Sub

The code above will export all the VBA components/modules in your ActiveWorkbook to the same location as your workbook. It will use the component name as part of the file name and will add a timestamp. Since you have more than 100 modules, you'd better change the export path to include a subfolder to group them all together in one place.

NOTE: For this to work, you need to select Options>Trust Center>Trust Center Settings...>Macro Settings>Trust access to the VBA project object model. Otherwise you get some random error in the For Each line. You can tick that option off afterwards if you're worried about it.

Share:
12,206
Lamloumi Afif
Author by

Lamloumi Afif

Hi, I am a Software Development Engineer with a genuine interest in .Net framework. I enjoy reading books and practising sport. LinkedIn Viadeo

Updated on June 05, 2022

Comments

  • Lamloumi Afif
    Lamloumi Afif almost 2 years

    I have an ms access Application ( ms access 2013) and I'd like to export all vba code to separate files in a folder

    What code can i add to do this?

    Thanks,