How to export VBAProject to Excel

I have prepared a VBA project in Microsoft Excel with many custom forms and macros. I want to export all files, but it looks like you can only do it one by one, which will take me a very long time.

Is there a way to export the entire project? Thank!

+3


source to share


1 answer


Here is the VBA code I am using to export the VBA code:

'Requires Microsoft Visual Basic for Applications Extensibility
Private Function exportvba(Path As String)
Dim objVbComp As VBComponent
Dim strPath As String
Dim varItem As Variant
Dim fso As New FileSystemObject
Dim filename As String

filename = fso.GetFileName(Path)

On Error Resume Next
    MkDir ("C:\Create\directory\for\VBA\Code\" & filename & "\")
On Error GoTo 0

'Change the path to suit the users needs
strPath = "C:\Give\directory\to\save\Code\in\" & filename & "\"

  For Each varItem In ActiveWorkbook.VBProject.VBComponents
  Set objVbComp = varItem

  Select Case objVbComp.Type
     Case vbext_ct_StdModule
        objVbComp.Export strPath & "\" & objVbComp.name & ".bas"
     Case vbext_ct_Document, vbext_ct_ClassModule
        ' ThisDocument and class modules
        objVbComp.Export strPath & "\" & objVbComp.name & ".cls"
     Case vbext_ct_MSForm
        objVbComp.Export strPath & "\" & objVbComp.name & ".frm"
     Case Else
        objVbComp.Export strPath & "\" & objVbComp.name
  End Select
Next varItem
End Function

      



The Path variable passed is the path to the file from which you want to export the code. If you have multiple files, just use this function in a loop.

+2


source







All Articles