2

I have a set of ~10 modules/forms/classes that I like to use and reuse for many many projects. Usually these modules are coming in at the middle or the end of the project, not the start (So no making a template and using that from the start - I use that to make a few projects). Is there an easy way to mass import modules and references (that doesn't involve changing the file extension to .zip)?

I'm open to the file extension change method, but I'd like to exhaust my other resources first

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Selkie
  • 1,215
  • 1
  • 17
  • 34
  • 3
    [Rubberduck](http://rubberduckvba.com/) can bulk-export and -import. I have used the export, but haven't tried the import myself. – cxw May 02 '19 at 16:04
  • 2
    @cxw bulk-import was as simple as allowing multiple selections in the "import" file selection dialog ;-) – Mathieu Guindon May 02 '19 at 16:19

2 Answers2

5

You don't need any code or add-in for mass-importing. Simply save the commonly-used files in one specific folder that only contains these files.

It's a little-known VBE feature, that the Project Explorer supports drag-and-drop.

Then when you start a new VBA project, drag the files from the Windows Explorer and drop them onto the VBE's Project Explorer toolwindow - done.

The VBE's "import" file browse dialog is blocking multiple selections for some reason; Rubberduck fixes that by allowing multiple selections in its own "import" file browse dialog (off its Code Explorer toolwindow), and its "Export Active Project" tool makes it trivial to export an entire project's source code into a given folder like, say, a local git repository.

As for project references, no code can do that automatically without parsing some metadata that would have to be included somewhere in the module itself. Adding project references is annoying... Rubberduck's add/remove references dialog makes it much easier:

Rubberduck add/remove references dialog, with libraries filtered for "scripting" Disclaimer: I manage the Rubberduck open-source project.

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
2

You can do it from vba code ,

i get this code from here : https://www.rondebruin.nl/win/s9/win002.htm

Public Sub ImportModules()
    Dim wkbTarget As Excel.Workbook
    Dim objFSO As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim szTargetWorkbook As String
    Dim szImportPath As String
    Dim szFileName As String
    Dim cmpComponents As VBIDE.VBComponents

    If ActiveWorkbook.Name = ThisWorkbook.Name Then
        MsgBox "Select another destination workbook" & _
        "Not possible to import in this workbook "
        Exit Sub
    End If

    'Get the path to the folder with modules
    If FolderWithVBAProjectFiles = "Error" Then
        MsgBox "Import Folder not exist"
        Exit Sub
    End If

    ''' NOTE: This workbook must be open in Excel.
    szTargetWorkbook = ActiveWorkbook.Name
    Set wkbTarget = Application.Workbooks(szTargetWorkbook)

    If wkbTarget.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
        "not possible to Import the code"
    Exit Sub
    End If

    ''' NOTE: Path where the code modules are located.
    szImportPath = FolderWithVBAProjectFiles & "\"

    Set objFSO = New Scripting.FileSystemObject
    If objFSO.GetFolder(szImportPath).Files.Count = 0 Then
       MsgBox "There are no files to import"
       Exit Sub
    End If

    'Delete all modules/Userforms from the ActiveWorkbook
    Call DeleteVBAModulesAndUserForms

    Set cmpComponents = wkbTarget.VBProject.VBComponents

    ''' Import all the code modules in the specified path
    ''' to the ActiveWorkbook.
    For Each objFile In objFSO.GetFolder(szImportPath).Files

        If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
            (objFSO.GetExtensionName(objFile.Name) = "frm") Or _
            (objFSO.GetExtensionName(objFile.Name) = "bas") Then
            cmpComponents.Import objFile.Path
        End If

    Next objFile

    MsgBox "Import is ready"
End Sub
    Public Sub ExportModules()
        Dim bExport As Boolean
        Dim wkbSource As Excel.Workbook
        Dim szSourceWorkbook As String
        Dim szExportPath As String
        Dim szFileName As String
        Dim cmpComponent As VBIDE.VBComponent

        ''' The code modules will be exported in a folder named.
        ''' VBAProjectFiles in the Documents folder.
        ''' The code below create this folder if it not exist
        ''' or delete all files in the folder if it exist.
        If FolderWithVBAProjectFiles = "Error" Then
            MsgBox "Export Folder not exist"
            Exit Sub
        End If

        On Error Resume Next
            Kill FolderWithVBAProjectFiles & "\*.*"
        On Error GoTo 0

        ''' NOTE: This workbook must be open in Excel.
        szSourceWorkbook = ActiveWorkbook.Name
        Set wkbSource = Application.Workbooks(szSourceWorkbook)

        If wkbSource.VBProject.Protection = 1 Then
        MsgBox "The VBA in this workbook is protected," & _
            "not possible to export the code"
        Exit Sub
        End If

        szExportPath = FolderWithVBAProjectFiles & "\"

        For Each cmpComponent In wkbSource.VBProject.VBComponents

            bExport = True
            szFileName = cmpComponent.Name

            ''' Concatenate the correct filename for export.
            Select Case cmpComponent.Type
                Case vbext_ct_ClassModule
                    szFileName = szFileName & ".cls"
                Case vbext_ct_MSForm
                    szFileName = szFileName & ".frm"
                Case vbext_ct_StdModule
                    szFileName = szFileName & ".bas"
                Case vbext_ct_Document
                    ''' This is a worksheet or workbook object.
                    ''' Don't try to export.
                    bExport = False
            End Select

            If bExport Then
                ''' Export the component to a text file.
                cmpComponent.Export szExportPath & szFileName

            ''' remove it from the project if you want
            '''wkbSource.VBProject.VBComponents.Remove cmpComponent

            End If

        Next cmpComponent

        MsgBox "Export is ready"
    End Sub




Function FolderWithVBAProjectFiles() As String
    Dim WshShell As Object
    Dim FSO As Object
    Dim SpecialPath As String

    Set WshShell = CreateObject("WScript.Shell")
    Set FSO = CreateObject("scripting.filesystemobject")

    SpecialPath = WshShell.SpecialFolders("MyDocuments")

    If Right(SpecialPath, 1) <> "\" Then
        SpecialPath = SpecialPath & "\"
    End If

    If FSO.FolderExists(SpecialPath & "VBAProjectFiles") = False Then
        On Error Resume Next
        MkDir SpecialPath & "VBAProjectFiles"
        On Error GoTo 0
    End If

    If FSO.FolderExists(SpecialPath & "VBAProjectFiles") = True Then
        FolderWithVBAProjectFiles = SpecialPath & "VBAProjectFiles"
    Else
        FolderWithVBAProjectFiles = "Error"
    End If

End Function

Function DeleteVBAModulesAndUserForms()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent

        Set VBProj = ActiveWorkbook.VBProject

        For Each VBComp In VBProj.VBComponents
            If VBComp.Type = vbext_ct_Document Then
                'Thisworkbook or worksheet module
                'We do nothing
            Else
                VBProj.VBComponents.Remove VBComp
            End If
        Next VBComp
End Function
Ronan Vico
  • 585
  • 3
  • 9