Skip to content

Instantly share code, notes, and snippets.

@windwiny
Created December 8, 2013 09:48
Show Gist options
  • Save windwiny/7855281 to your computer and use it in GitHub Desktop.
Save windwiny/7855281 to your computer and use it in GitHub Desktop.
Extract VBA code to text files (for diffing)
Option Explicit
Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
' Extract VBA code to text files (for diffing)
Public Sub export_vba()
Dim Exportfolder As String
Dim Exportfile As String
Dim Sfx As String
Dim VBComp
Const vbext_ct_ClassModule = 2
Const vbext_ct_Document = 100
Const vbext_ct_MSForm = 3
Const vbext_ct_StdModule = 1
Exportfolder = GetFolder("c:\")
If Exportfolder = "" Then
MsgBox "Not select a folder"
Exit Sub
End If
Debug.Print "== Begin Extract VBA code:"
For Each VBComp In Application.ActiveWorkbook.VBProject.VBComponents
Select Case VBComp.Type
Case vbext_ct_ClassModule, vbext_ct_Document
Sfx = ".cls"
Case vbext_ct_MSForm
Sfx = ".frm"
Case vbext_ct_StdModule
Sfx = ".bas"
Case Else
Sfx = ""
End Select
If Sfx <> "" Then
On Error Resume Next
Err.Clear
Exportfile = Exportfolder & "\" & VBComp.name & Sfx
Debug.Print Exportfile
VBComp.export Exportfile
If Err.Number <> 0 Then
MsgBox "Failed to export " & Exportfile
End If
On Error GoTo 0
End If
Next
Debug.Print "== End Extract VBA code."
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment