sakai-memoru
3/21/2020 - 1:52 AM

Tool_ModuleExporter.cls

For Excel VBA, To export module files into the current folder.

Option Explicit
''' /****************************************
'''  * @file Tool_ModuleExporter.cls
'''  * @version 1.00
'''  * @since 2020/03/31
'''  */
'
''' /********************************************************
'''  * @class T_ModuleExporter
'''  * @classdesc Module Exporter
'''  * @require C_File, C_Date
'''  */


Private Sub Class_Initialize()
''' /********************************************************
'''  * @event Class_Initialize
'''  */
End Sub

Private Sub Class_Terminate()
''' /********************************************************
'''  * @event Class_Terminate
'''  */
'Destructor
    Set C_Date = Nothing
    Set C_File = Nothing
End Sub

'//---------------------------------------
'// Instance method
'

Public Sub export()
''' /********************************************************
'''  * @function Export
'''  *
'''  * @require Make VBA object trusted to access in security center
'''  * @require C_Date, C_File
'''
    ' const
    Const CONS_export_path As String = "vbaProject"
    Const CONS_StdModule As Integer = 1
    Const CONS_StdModuleFolder As String = "Modules"
    Const CONS_ClassModule As Integer = 2
    Const CONS_ClassModuleFolder As String = "ClassModules"
    ' variables
    Dim objVbComponent As Object
    Dim strExtention As String
    Dim strFilePath As String
    Dim strFolderPath As String
    Dim strExportPath As String
    Dim C_Date As C_Date
    Set C_Date = New C_Date
    Dim C_File As C_File
    Set C_File = New C_File
    '
    Dim strDtmformat As String
    strDtmformat = C_Date.getYYMMDDhhnnss
    Dim currentPath As String
    currentPath = C_File.getCurrentDirectory
    '
    strExportPath = C_File.buildPath(currentPath, CONS_export_path) & "_" & strDtmformat
    C_File.createFolder (strExportPath)
    C_File.createFolder (C_File.buildPath(strExportPath, CONS_StdModuleFolder))
    C_File.createFolder (C_File.buildPath(strExportPath, CONS_ClassModuleFolder))
    '
    For Each objVbComponent In ThisWorkbook.VBProject.VBComponents
        ' Debug.Print objVbComponent.name
        If objVbComponent.Type <= 2 Then
            Select Case objVbComponent.Type
                Case CONS_StdModule
                    strExtention = ".bas"
                    strFolderPath = C_File.buildPath(strExportPath, CONS_StdModuleFolder)
                    
                Case CONS_ClassModule
                    strExtention = ".cls"
                    strFolderPath = C_File.buildPath(strExportPath, CONS_ClassModuleFolder)
            End Select
            
            strFilePath = C_File.buildPath(strFolderPath, objVbComponent.name & strExtention)
            Console.log (strFilePath)
            Call objVbComponent.export(strFilePath)
        End If
    Next
    '
    Set objVbComponent = Nothing
    Set C_Date = Nothing
    Set C_File = Nothing
    '
End Sub

'/////////////////////////////////////////////////////////////
'//  Debug Area  /////////////////////////////////////////////
'/////////////////////////////////////////////////////////////
'-------------------------------------------------------------
' debug entry
'-------------------------------------------------------------
Public Sub unitTest()
    export
End Sub