Excel - VBA

VBA Project 소스코드 백업하기

EGTools 2024. 5. 18. 23:14
728x90

VBA로 프로그램 진행하고, 버전마다 소스 백업을 할 경우에

일일이 모듈별로 내보내기 작업을 했었는데,

모듈수가 많아지다보니 귀찮아져서,,,

이것도 자동으로 하는 매크로를 만들어 봅니다.

 

모듈을 자동화할 때에는 아래 리소스를 참조에 추가해야 합니다.

Microsoft Visual Basic for Application Extensibility 5.3

 

물론, 개발이 끝나면 Late Bind로 처리하고 참조에서 제외하여 사용성을 고려합니다.

 

소스코드가 저장되는 위치는 이 매크로가 실행되는 파일이 있는 폴더 하위에

"Source"  폴더를 만들고, 그 아래 버전별로 폴더를 만들어 Module, Class, WorkSheet 들을 보관합니다.

 

Public Const ThisVersion As String = "4.2.4"

Sub BackUpDodules()
    '// 참조(References) : Microsoft Visual Basic for Application Extensibility 5.3
    'Dim oPRJ As VBProject, oMOD As VBComponent, oCOD As CodeModule
    Dim oPRJ As Object, oMOD As Object, oCOD As Object
    Dim WB As Workbook, sPath As String, sFileName As String
    Dim sEXT As String, vLines As Variant, sContents As String
    Dim bAlert As Boolean, bScreen As Boolean
    
    Const vbext_pp_locked As Integer = 1
    Const vbext_ct_StdModule As Integer = 1
    Const vbext_ct_classmodule As Integer = 2
    Const vbext_ct_MSForm As Integer = 3
    Const vbext_ct_Document As Integer = 100
    
    Set WB = ThisWorkbook
    Set oPRJ = WB.VBProject
    bScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    sPath = WB.Path & Application.PathSeparator & "Sources"
    If Dir(sPath, vbDirectory) = "" Then MkDir sPath
    sPath = sPath & Application.PathSeparator & "v" & Replace(ThisVersion, ".", "")
    If Dir(sPath, vbDirectory) = "" Then MkDir sPath
    
    For Each oMOD In oPRJ.VBComponents
        If Dir(sPath, vbDirectory) = "" Then MkDir sPath
        Select Case oMOD.Type
        Case vbext_ct_classmodule, vbext_ct_Document: sEXT = ".cls"
        Case vbext_ct_MSForm:                         sEXT = ".frm"
        Case vbext_ct_StdModule:                      sEXT = ".bas"
        Case Else
        End Select
        If oMOD.CodeModule.CountOfLines > 1 Then oMOD.Export sPath & Application.PathSeparator & oMOD.Name & sEXT
    Next oMOD
    
    WB.Worksheets.Copy
    bAlert = Application.DisplayAlerts
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs sPath & Application.PathSeparator & "Sheets.xlsx", , , , , , , xlLocalSessionChanges
    ActiveWorkbook.Close
    Application.DisplayAlerts = bAlert
    WB.Activate
    
    Set oCOD = Nothing
    Set oMOD = Nothing
    Set oPRJ = Nothing
    Set WB = Nothing
    Application.ScreenUpdating = bScreen
    
End Sub
728x90