Replace picture size and location

noelmus

Board Regular
Joined
Dec 30, 2018
Messages
105
The code below works fine but I can't modify it so that the new picture will have this location and size:
.Top = Rows(1).Top
.Left = Columns(1).Left
.Height = 2.69cm
.Width = 3.59cm

Any help is appreciated.

VBA Code:
Sub change_picture()
    ' Change as needed, keep the \ at the end
    Const strFolder = "C:\Users\mars\Desktop\Dog\"
    Const strPic = "Picture 5"
    Const strPicFile = "C:\Users\mars\Desktop\cat.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("C2", "C4", "C6"))
            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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What happens if you delete below at the beginning?
VBA Code:
Dim t As Single, l As Single, h As Single, w As Single
 
Upvote 0
I remove the line
VBA Code:
Dim t As Single, l As Single, h As Single, w As Single
and the new picture took the size and location of the old picture.
Convert cm to what?
Thanks in advance
 
Upvote 0
I haven't checked or used this, popped up when I googled.
.Height = Application.CentimetersToPoints(2.69)
.Width = Application.CentimetersToPoints(3.59)
 
Upvote 0
Mr Moore, I managed to do it by the help of a friend:
VBA Code:
Sub change_picture()
    ' Change as needed, keep the \ at the end
    Const strFolder = "C:\Users\mars\Desktop\Dog\"
    Const strPic = "Picture 5"
    Const strPicFile = "C:\Users\mars\Desktop\cat.jpg"
    Dim strFile As String
    Dim wbk As Workbook
    Dim wsh As Worksheet
    Dim f As Boolean
    Dim shp As Shape
  
    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("C2", "C4", "C6"))
            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
            With wsh.Pictures.Insert(strPicFile).ShapeRange 'Insert picture
             .LockAspectRatio = msoTrue
             .Width = 40
             .Left = wsh.Columns(1).Left
             .Top = wsh.Rows(1).Top
             .Name = strPic
        End With
            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
Thanks for trying to help me.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top