Sub JustDoIt()
'
' constants
Const ksExtFrm = ".frm"
Const ksExtFrx = ".frx"
Const ksExtBas = ".bas"
Const ksAsterix = "*"
'
' declarations
Dim sForm() As String, sModule() As String, bComponents() As Boolean
Dim iForm As Integer, iModule As Integer
Dim iRead As Integer, iWrite As Integer, sFolder As String
Dim I As Integer, J As Integer, A As String, bOk As Boolean
'
' start
' validation
Validating bOk
If Not bOk Then Exit Sub
' create backup subfolder
sFolder = Format(Now(), "yyyymmdd_hhmmss")
MkDir gsPath(2) & sFolder
' template
With ThisWorkbook.VBProject
iForm = 0
iModule = 0
ReDim bComponents(.VBComponents.Count)
For I = 1 To .VBComponents.Count
With .VBComponents(I)
bComponents(I) = False
A = .Name
Select Case .Type
Case vbext_ct_MSForm
If .Name <> gksFormAdmin Then
iForm = iForm + 1
ReDim Preserve sForm(iForm)
sForm(iForm) = A
bComponents(I) = True
.Export gsPath(2) & sFolder & Application.PathSeparator & _
A & ksExtFrm
End If
Case vbext_ct_StdModule
If .Name <> gksModuleAdmin Then
iModule = iModule + 1
ReDim Preserve sModule(iModule)
sModule(iModule) = A
bComponents(I) = True
.Export gsPath(2) & sFolder & Application.PathSeparator & _
A & ksExtBas
End If
End Select
End With
Next I
End With
'
' process
iRead = 0
iWrite = 0
If bOk Then
A = Dir(gsPath(1) & gsFileName)
Do Until A = ""
If A <> ThisWorkbook.Name Then
' count
iRead = iRead + 1
' backup
FileCopy gsPath(1) & A, _
gsPath(2) & sFolder & Application.PathSeparator & A
' open
On Error Resume Next
Workbooks.Open gsPath(1) & A, False
If Err.Number <> 0 Then
Err.Clear
Else
' main
iWrite = iWrite + 1
' delete old components
With ActiveWorkbook.VBProject
For I = .VBComponents.Count To 1 Step -1
With .VBComponents(I)
A = .Name
Select Case .Type
Case vbext_ct_MSForm
For J = 1 To iForm
If A = sForm(J) Then Exit For
Next J
If J <= iForm Or J > iForm And gbFormDelete Then
' delete form
Debug.Print ActiveWorkbook.Name, "delete form", A
ActiveWorkbook.VBProject.VBComponents.Remove _
ActiveWorkbook.VBProject.VBComponents(I)
End If
Case vbext_ct_StdModule
For J = 1 To iModule
If A = sModule(J) Then Exit For
Next J
If J <= iModule Or J > iModule And gbModuleDelete Then
' delete module
Debug.Print ActiveWorkbook.Name, "delete module", A
ActiveWorkbook.VBProject.VBComponents.Remove _
ActiveWorkbook.VBProject.VBComponents(I)
End If
End Select
End With
Next I
End With
' add new components
With ThisWorkbook.VBProject
For I = 1 To .VBComponents.Count
With .VBComponents(I)
If bComponents(I) Then
A = .Name
Select Case .Type
Case vbext_ct_MSForm
' add form
Debug.Print ActiveWorkbook.Name, "add form", A
ActiveWorkbook.VBProject.VBComponents.Import _
gsPath(2) & sFolder & Application.PathSeparator & _
A & ksExtFrm
Case vbext_ct_StdModule
' add module
Debug.Print ActiveWorkbook.Name, "add module", A
ActiveWorkbook.VBProject.VBComponents.Import _
gsPath(2) & sFolder & Application.PathSeparator & _
A & ksExtBas
End Select
End If
End With
Next I
End With
' save
ActiveWorkbook.Close True
End If
On Error GoTo 0
End If
' cycle
A = Dir()
Loop
' cleanup
On Error Resume Next
Kill gsPath(2) & sFolder & Application.PathSeparator & ksAsterix & ksExtFrm
Kill gsPath(2) & sFolder & Application.PathSeparator & ksAsterix & ksExtFrx
Kill gsPath(2) & sFolder & Application.PathSeparator & ksAsterix & ksExtBas
On Error GoTo 0
' msg
MsgBox CStr(iWrite) & " files updated from " & CStr(iRead) & " files matching", _
vbApplicationModal + IIf(iWrite = iRead, vbInformation, vbExclamation) + vbOKOnly, _
"Summary"
End If
'
' end
Unload ufAdmin
Beep
'
End Sub