Option Explicit
Public Sub Main()
Dim stCalc As Integer
Dim strPath As String
On Error GoTo Fin
With Application
.ScreenUpdating = False
.AskToUpdateLinks = False
.EnableEvents = False
stCalc = .Calculation
.Calculation = xlCalculationManual
.DisplayAlerts = False
End With
' File is in the same directory as the file with the logos
strPath = ThisWorkbook.Path & Application.PathSeparator
' File is in certain directory
' strPath = "C:\Temp\" ' adapt
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
' SearchFiles strPath, "*.xls*", False ' without subfolder
SearchFiles strPath, "*.xls*", True ' with subfolder
Fin:
With Application
.ScreenUpdating = True
.AskToUpdateLinks = True
.EnableEvents = True
.Calculation = stCalc
.DisplayAlerts = True
End With
If Err.Number <> 0 Then MsgBox "Error: " & _
Err.Number & " " & Err.Description
End Sub
Private Sub SearchFiles(strFolder As String, strFileName As String, _
Optional blnTMP As Boolean = False)
Dim wksSheet As Worksheet
Dim objFolder As Object
Dim sngHeight As Single
Dim sngWidth As Single
Dim strLogo1 As String
Dim strLogo2 As String
Dim sngLeft As Single
Dim shpShape As Shape
Dim objFile As Object
Dim sngTop As Single
Dim objFSO As Object
strLogo1 = ThisWorkbook.Path & Application.PathSeparator & "NEW_LOGO_1.jpg"
strLogo2 = ThisWorkbook.Path & Application.PathSeparator & "NEW_LOGO_2.jpg"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strFolder).Files
If objFile.Name Like strFileName And objFile.Name <> _
ThisWorkbook.Name And Left(objFile.Name, 1) <> "~" Then
Workbooks.Open objFile.Path
For Each wksSheet In Workbooks(objFile.Name).Worksheets
For Each shpShape In wksSheet.Shapes
With shpShape
If .TopLeftCell.Row > 4 Then
If .TopLeftCell.Row > 37 Then
sngHeight = .Height
sngWidth = .Width
sngTop = .Top
sngLeft = .Left
.Delete
Set shpShape = wksSheet.Shapes.AddPicture(strLogo2, _
True, True, sngLeft, sngTop, _
-1, -1)
With shpShape
.Name = "Logo2"
.LockAspectRatio = msoTrue
.Width = sngWidth
.Height = sngHeight
End With
Else
sngHeight = .Height
sngWidth = .Width
sngTop = .Top
sngLeft = .Left
.Delete
Set shpShape = wksSheet.Shapes.AddPicture(strLogo1, _
True, True, sngLeft, sngTop, _
-1, -1)
With shpShape
.Name = "Logo1"
.LockAspectRatio = msoTrue
.Width = sngWidth
.Height = sngHeight
End With
End If
ElseIf .TopLeftCell.Row < 4 Then
If .TopLeftCell.Column < 4 Then
sngHeight = .Height
sngWidth = .Width
sngTop = .Top
sngLeft = .Left
.Delete
Set shpShape = wksSheet.Shapes.AddPicture(strLogo2, _
True, True, sngLeft, sngTop, _
-1, -1)
With shpShape
.Name = "Logo2"
.LockAspectRatio = msoTrue
.Width = sngWidth
.Height = sngHeight
End With
Else
sngHeight = .Height
sngWidth = .Width
sngTop = .Top
sngLeft = .Left
.Delete
Set shpShape = wksSheet.Shapes.AddPicture(strLogo1, _
True, True, sngLeft, sngTop, _
-1, -1)
With shpShape
.Name = "Logo1"
.LockAspectRatio = msoTrue
.Width = sngWidth
.Height = sngHeight
End With
End If
End If
End With
Set shpShape = Nothing
Next shpShape
Next wksSheet
Workbooks(objFile.Name).Close True
End If
Next objFile
If blnTMP = True Then
For Each objFolder In objFSO.GetFolder(strFolder).Subfolders
If Right(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
SearchFiles strFolder & "\" & objFolder.Name, strFileName, blnTMP
Next objFolder
End If
End Sub