I have used the following to export a project to files and import into a
new workbook.
Note that you need to reference Microsoft Scripting Runtime and
Microsoft Visual Basic for Applications.
First, export the project from the lastest addition of your project
using the ExportProject macro. Second, open a new workbook and import
the ExportProjectModule.bas. Delete the ExportProjectModule.bas from
the "VBACode" directory so it doesn't get imported twice. Lastly run
the ImportProject macro.
I'm sure there's room for improvment on this, but I've gotten lazy.
Also I haven't found a way to delete modules from an existing workbook.
If any one know how, please let me know.
Attribute VB_Name = "ExportProjectModule"
Option Explicit
' Requires Microsoft Scripting Runtime
' Requires Microsoft Visual Basic for Applications Extensibility
Private Enum FILE_TYPE
MODULE_TYPE = 1
CLASS_TYPE = 2
FORM_TYPE = 3
End Enum
Const strPath = "C:\RPP\Test\VBACode\"
Public Sub ExportProject()
Dim fsoFileSystemObject As FileSystemObject
Dim vbComp As VBComponent
Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")
If Not fsoFileSystemObject.FolderExists(strPath) Then
MsgBox "Folder does not exist <" + strPath + ">"
Exit Sub
End If
For Each vbComp In ThisWorkbook.VBProject.VBComponents
Select Case vbComp.Type
Case FILE_TYPE.MODULE_TYPE
vbComp.Export strPath + vbComp.Name + ".bas"
Case FILE_TYPE.CLASS_TYPE
vbComp.Export strPath + vbComp.Name + ".cls"
Case FILE_TYPE.FORM_TYPE
vbComp.Export strPath + vbComp.Name + ".frm"
End Select
Next
Set fsoFileSystemObject = Nothing
Set vbComp = Nothing
End Sub
Public Sub ImportProject()
Dim fsoFileSystemObject As FileSystemObject
Dim fFolder As Folder
Dim fFile As File
Dim vbComp As VBComponent
Set fsoFileSystemObject = CreateObject("Scripting.FileSystemObject")
If Not fsoFileSystemObject.FolderExists(strPath) Then
MsgBox "Folder does not exist <" + strPath + ">"
Exit Sub
End If
Set fFolder = fsoFileSystemObject.GetFolder(strPath)
For Each fFile In fFolder.Files
If fFile.Name = "ExportProjectModule.bas" Then
' Skip this file
ElseIf InStr(fFile.Name, ".bas") Then
ThisWorkbook.VBProject.VBComponents.Import strPath + "\" +
fFile.Name
ElseIf InStr(fFile.Name, ".cls") Then
ThisWorkbook.VBProject.VBComponents.Import strPath + "\" +
fFile.Name
ElseIf InStr(fFile.Name, ".frm") Then
ThisWorkbook.VBProject.VBComponents.Import strPath + "\" +
fFile.Name
End If
Next
Set fsoFileSystemObject = Nothing
Set fFolder = Nothing
Set fFile = Nothing
Set vbComp = Nothing
End Sub