Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcucciboy2/204a8a27e2179a7dae1aee40c543e4de to your computer and use it in GitHub Desktop.
Save marcucciboy2/204a8a27e2179a7dae1aee40c543e4de to your computer and use it in GitHub Desktop.
Excel macro to export all VBA source code in this project to text files for proper source control versioning
' Excel macro to export all VBA source code in this project to text files for proper source control versioning
' Requires enabling the Excel setting in Options/Trust Center/Trust Center Settings/Macro Settings/Trust access to the VBA project object model
' changes:
' - only exports normal modules
' - uses mkdir instead of fso
' - does not use statusbar
Public Sub ExportVisualBasicCode()
Dim directory As String
directory = "C:\"
If Len(Dir(directory, vbDirectory)) = 0 Then
MkDir directory
End If
Dim VBComponent As Object
Dim path As String
For Each VBComponent In ActiveWorkbook.VBProject.VBComponents
' Module = 1 '".bas"
' ClassModule = 2 '".cls"
' Form = 3 '".frm"
' Document = 100 '".cls"
' Else '".txt"
If VBComponent.Type = 1 Then
On Error Resume Next
Err.Clear
path = directory & "\" & VBComponent.Name & ".bas"
Call VBComponent.Export(path)
If Err.Number <> 0 Then
Call MsgBox("Failed to export " & VBComponent.Name & " to " & path, vbCritical)
Else
Debug.Print Trim$("Exported " & VBComponent.Name & ": " & path)
End If
On Error GoTo 0
End If
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment