Hello everyone. This is my first time posting here. I have a workbook that my firm has been using over the past two years. This workbook has macros in it and we have been updating those macros, from time to time, by creating an updating workbook that replaces the modules in each workbook. We are now at a point where we need to update over 5,000 of these workbooks and would like to do this automatically without having someone open each one and run the updater. I've put together the code below with the help from a book and Google. When testing it on a folder that contains six files it works perfectly for file 2, 4, and 6. For the other files it imports the new module but fails to remove the old module. Can anyone help me determine where this is messing up? All help is greatly appreciated.
Thank You.
Thank You.
Code:
Sub Update_Workbooks()
'This macro requires that a reference to Microsoft Scripting Routine
'be selected under Tools\References in order for it to work.
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim fso As New FileSystemObject
Dim source As Scripting.Folder
Dim wbFile As Scripting.File
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim Filename As String
Dim ModuleFile As String
Set source = fso.GetFolder("C:\Users\Desktop\Testing") 'we will know this since all of the files will be in one folder
For Each wbFile In source.Files
If fso.GetExtensionName(wbFile.Name) = "xlsm" Then 'we will konw this too. All files will be .xlsm
Set book = Workbooks.Open(wbFile.path)
Filename = FileNameOnly(wbFile.Name)
On Error GoTo ErrHandle
' Export Module1 from updating workbook
ModuleFile = Application.DefaultFilePath & "\tempmodxxx.bas"
Workbooks("Update Multiple Workbooks.xlsm").VBProject.VBComponents("Module1") _
.Export ModuleFile
' Remove existing modules. Will either be Module1 or Module11
On Error Resume Next
Set VBP = Workbooks(Filename).VBProject
With VBP.VBComponents
.Remove VBP.VBComponents("Module1")
.Remove VBP.VBComponents("Module11")
End With
' Replace Module1 in Userbook
Set VBP = Workbooks(Filename).VBProject
On Error Resume Next
With VBP.VBComponents
.Import ModuleFile
End With
' Delete the temporary module file
Kill ModuleFile
book.Close True
End If
Next
Exit Sub
ErrHandle:
' Did an error occur?
MsgBox "ERROR. The module may not have been replaced.", _
vbCritical
End Sub