Replace pictures vba

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Replace pictures vba

Post by noelmus »

Hello,
The code below is replacing a picture with another in only one sheet. How to modify this code to replace all pictures ( all named "Picture 2") in a folder with 15 workbooks where each workbook has 3 sheets ( named "Pic1", "Pic2" and "Pic3" ) with this picture named "Picture 2".

Regards.

Code: Select all

'change picture without change image size
Sub change_picture()
strPic = "Picture 2"
Set shp = Worksheets(1).Shapes(strPic)

'Capture properties of exisitng picture such as location and size
With shp
    t = .Top
    l = .Left
    h = .Height
    w = .Width
End With

Worksheets(1).Shapes(strPic).Delete

Set shp = Worksheets(1).Shapes.AddPicture("C:\Users\mike\Desktop\lastpic\.1\2.jpg", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic

End Sub

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

Welcome to Eileen's Lounge!

Try this macro:

Code: Select all

Sub change_picture()
    ' Change as needed, keep the \ at the end
    Const strFolder = "C:\Excel\"
    Const strPic = "Picture 2"
    Const strPicFile = "C:\Users\mike\Desktop\lastpic\.1\2.jpg"
    Dim strFile As String
    Dim wbk As Workbook
    Dim wsh As Worksheet
    Dim shp As Shape
    Dim t As Single, l As Single, h As Single, w As Single
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    strFile = Dir(strFolder & "*.xls*")
    Do While strFile <> ""
        Set wbk = Workbooks.Open(Filename:=strFolder & strFile, UpdateLinks:=False)
        For Each wsh In Worksheets(Array("Pic1", "Pic2", "Pic3"))
            Set shp = wsh.Shapes(strPic)
            'Capture properties of existing picture such as location and size
            With shp
                t = .Top
                l = .Left
                h = .Height
                w = .Width
                .Delete
            End With
            Set shp = wsh.Shapes.AddPicture(strPicFile, msoFalse, msoTrue, l, t, w, h)
            shp.Name = strPic
        Next wsh
        wbk.Close SaveChanges:=True
        strFile = Dir
    Loop
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Hello
Code worked brilliant.
Let say I have also sheets "Pic4" and "Pic5" with picture named "Picture 2", do I have to do like the line below?

Code: Select all

For Each wsh In Worksheets(Array("Pic1", "Pic2", "Pic3", "Pic4", "Pic5"))
.

Thanks and appreciate

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

Yes, Noel (Bomba?)
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Noel is my brother and I'm using his PC since mine is for service. Actually he told me with this forum.
Regards

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Hello,
I added another sheet "Pic4" which is protected and gave me an error:
(The specified value is out of range.) and line ".Delete" became highlighted. I tried to find if I have a misspelling but I found nothing.
Do you think because sheet "Pic4" is protected?
Thanks

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

Yes, that is the reason. Has the sheet been protected with a password?
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

No.

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

The following version will unprotect the sheet temporarily, then protect it again after replacing the shape.

Code: Select all

Sub change_picture()
    ' Change as needed, keep the \ at the end
    Const strFolder = "C:\Excel\"
    Const strPic = "Picture 2"
    Const strPicFile = "C:\Users\mike\Desktop\lastpic\.1\2.jpg"
    Dim strFile As String
    Dim wbk As Workbook
    Dim wsh As Worksheet
    Dim f As Boolean
    Dim shp As Shape
    Dim t As Single, l As Single, h As Single, w As Single
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    strFile = Dir(strFolder & "*.xls*")
    Do While strFile <> ""
        Set wbk = Workbooks.Open(Filename:=strFolder & strFile, UpdateLinks:=False)
        For Each wsh In Worksheets(Array("Pic1", "Pic2", "Pic3", "Pic4", "Pic5"))
            If wsh.ProtectContents Then
                f = True
                wsh.Unprotect
            End If
            Set shp = wsh.Shapes(strPic)
            'Capture properties of existing picture such as location and size
            With shp
                t = .Top
                l = .Left
                h = .Height
                w = .Width
                .Delete
            End With
            Set shp = wsh.Shapes.AddPicture(strPicFile, msoFalse, msoTrue, l, t, w, h)
            shp.Name = strPic
            If f Then
                wsh.Protect
                f = False
            End If
        Next wsh
        wbk.Close SaveChanges:=True
        strFile = Dir
    Loop
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Thanks and appreciate.

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Is there an explanation why only one picture is replaced when there are 2 pictures with the same name in one sheet?
Regards

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

The line

Set shp = wsh.Shapes(strPic)

sets shp to the first shape with the specified name that it finds. It's a bad idea to have multiple shapes with the same name - you never know which one you're dealing with.
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

I changed the name of the second picture from "Picture 2" to "Picture 1", I changed the line
Const strPic = "Picture 2" to
Const strPic = "Picture 1". I run the macro again and the line
Set shp = wsh.Shapes(strPic) became highlighted. Is that because the code always finds the first picture?
Regards

User avatar
HansV
Administrator
Posts: 78464
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Replace pictures vba

Post by HansV »

If all the sheets Pic1 to Pic5 contain a picture named Picture 1, the code should work.
If you forgot to rename it in one or more of those sheets, you'd get an error.
Best wishes,
Hans

noelmus
NewLounger
Posts: 12
Joined: 12 Sep 2020, 15:08

Re: Replace pictures vba

Post by noelmus »

Thanks for your information.
Regards