How to export VBA code


The ExportVBAFiles VBA macro exports to disk all the modules, class modules, and forms contained in the VBA project associated with the currently loaded document. The files will be saved to a folder in the same location as the document. The folder will be named the same as the document name suffixed by "_VBACode".

How to use

  1. Paste this code into a module in the Visual Basic Editor for ArcMap or ArcCatalog.
  2. Run the ExportVBAFiles macro.
  3. A message box showing the path where the VBA files were exported to is displayed.
[VBA]
Sub ExportVBAFiles()
    Dim pVBAProject As VBProject
    Dim vbComp As VBComponent 'VBA module, form, etc...
    Dim strDocPath As String 'Current document path
    Dim strSavePath As String 'Path to save the exported files to
    
    ' strSavePath will be the pathname of the document with a _VBACode suffix
    ' If you want to export the code for Normal instead, change the following
    ' line to:
    ' strDocPath = Application.Templates.Item(0)
    strDocPath = Application.Templates.Item(Application.Templates.Count - 1)
    
    strSavePath = Left(strDocPath, Len(strDocPath) - 4)
    strSavePath = strSavePath & "_VBACode"
    
    ' If this folder doesn't exist, create it
    If Dir(strSavePath, vbDirectory) = "" Then
        MkDir strSavePath
    End If
    
    ' Get the VBA project
    ' If you want to export code for Normal instead, paste this macro into
    ' ThisDocument in the Normal VBA project and change the following line to:
    ' Set pVBAProject = ThisDocument.VBProject
    Set pVBAProject = Application.Document.VBProject
    
    ' Loop through all the components (modules, forms, etc) in the VBA project
    For Each vbComp In pVBAProject.VBComponents
        Select Case vbComp.Type
            Case vbext_ct_StdModule
                vbComp.Export strSavePath & "\" & vbComp.Name & ".bas"
            Case vbext_ct_Document, vbext_ct_ClassModule
                ' ThisDocument and class modules
                vbComp.Export strSavePath & "\" & vbComp.Name & ".cls"
            Case vbext_ct_MSForm
                vbComp.Export strSavePath & "\" & vbComp.Name & ".frm"
            Case Else
                vbComp.Export strSavePath & "\" & vbComp.Name
        End Select
    Next
    MsgBox "VBA files have been exported to: " & strSavePath
End Sub