Option Explicit
[I]'credit: https://gist.github.com/steve-jansen/7589478
' 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 mode[/I]l
[COLOR=#ff0000][/COLOR]
Sub ExportVisualBasicCode()
Const Module = 1
Const ClassModule = 2
Const Form = 3
Const Document = 100
Const Padding = 24
Dim VBComponent As Object
Dim count As Integer
Dim path As String
Dim Directory As String
Dim extension As String
Dim fso As New FileSystemObject
Directory = ActiveWorkbook.path & "\VisualBasic"
count = 0
If Not fso.FolderExists(Directory) Then
Call fso.CreateFolder(Directory)
End If
Set fso = Nothing
For Each VBComponent In ActiveWorkbook.VBProject.VBComponents
Select Case VBComponent.Type
Case ClassModule, Document
extension = ".cls"
Case Form
extension = ".frm"
Case Module
extension = ".bas"
Case Else
extension = ".txt"
End Select
On Error Resume Next
Err.Clear
path = Directory & "\" & VBComponent.Name & extension
Call VBComponent.Export(path)
If Err.Number <> 0 Then
Call MsgBox("Failed to export " & VBComponent.Name & " to " & path, vbCritical)
Else
count = count + 1
'[COLOR=#006400][I]remove apostrophe before next line to print list of files created to immediate window[/I][/COLOR]
'Debug.Print "Exported " & Left$(VBComponent.Name & ":" & Space(Padding), Padding) & path
End If
On Error GoTo 0
Next
Kill Directory & "\*.frx" 'these are not text files
Shell Environ$("COMSPEC") & " /c Copy " & Directory & "\*.* " & Directory & "\CombinedFile.txt "
End Sub