Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting VBA code from Multiple Excel documents to put into version control [closed]

Does anyone know a way to export the VBA code from a number of Excel documents, so that the code can be added into a subversion repository? Without having to manually open each document and export the code.

like image 734
Craig T Avatar asked Mar 04 '09 01:03

Craig T


1 Answers

You'll find a tool for this here:

http://www.pretentiousname.com/excel_extractvba/index.html

It's a VBS script that automates excel. You can modify it according to your needs - note that it isn't perfect (read the webpage for caveats).

option explicit

Const vbext_ct_ClassModule = 2
Const vbext_ct_Document = 100
Const vbext_ct_MSForm = 3
Const vbext_ct_StdModule = 1

Main

Sub Main
    Dim xl
    Dim fs
    Dim WBook
    Dim VBComp
    Dim Sfx
    Dim ExportFolder

    If Wscript.Arguments.Count <> 1 Then
        MsgBox "As the only argument, give the FULL path to an XLS file to extract all the VBA from it."
    Else

        Set xl = CreateObject("Excel.Application")
        Set fs = CreateObject("Scripting.FileSystemObject")

        xl.Visible = true

        Set WBook = xl.Workbooks.Open(Trim(wScript.Arguments(0)))

        ExportFolder = WBook.Path & "\" & fs.GetBaseName(WBook.Name)

        fs.CreateFolder(ExportFolder)

        For Each VBComp In WBook.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
                VBComp.Export ExportFolder & "\" & VBComp.Name & Sfx
                If Err.Number <> 0 Then
                    MsgBox "Failed to export " & ExportFolder & "\" & VBComp.Name & Sfx
                End If
                On Error Goto 0
            End If
        Next

        xl.Quit
    End If
End Sub

-Adam

like image 118
Adam Davis Avatar answered Nov 03 '22 05:11

Adam Davis