Kenrick Channata
Kenrick Channata

Reputation: 3

How to resize a single image in each slide in powerpoint using VBA?

I need help for this case and could not find an answer anywhere on the internet.

Basically, I have a powerpoint slide with hundreds of slides, each containing a picture. I would like this picture to be resized and moved to a specific location.

The code i made resized every picture on each slide.

'''

Sub resizeImage()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
        With shp
            .Height = 400
            .Width = 300
            .Left = 45
            .Top = 45
        End With

    Next sld
    Next shp
  End Sub 

'''

I thought that by specifying the image name to resize, i could esssentially resize only that picture since all of them have the name "Content Placeholder 2" and ended up with this code:

'''

Sub resizeImage()
Dim sld As Slide
Dim shp As Shape

For Each sld In ActivePresentation.Slides
    For Each shp In sld.Shapes
        If shp.Name = ("Content Placeholder 2") Then
            With shp
                .Height = 400
                .Width = 300
                .Left = 45
                .Top = 45
            End With
        End If
    Next sld
    Next shp

  End Sub 

'''

I'm guessing the code doesnt run because 'Content Placeholder 2' doesn't exist on the first few slides. But when i add On Error Resume Next, everything is resized. Thank you for any help or advice, greatly appreciated.

Upvotes: 0

Views: 93

Answers (1)

John Korchok
John Korchok

Reputation: 4913

"when i add On Error Resume Next, everything is resized" Are you telling use this solves the problem with the photo, or do you mean that all objects are resized, not just the photo? If the latter, you'll need some other identifier for the shape to be resized, just as its starting position. This code checks that the shape top is between 100 and 400, then resizes the shape if it meets those conditions:

Sub resizeImage()
    Dim sld As Slide
    Dim shp As Shape

    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Top > 100 And shp.Top < 400 Then
                With shp
                    .Height = 400
                    .Width = 300
                    .Left = 45
                    .Top = 45
                End With
            End If
        Next sld
    Next shp
End Sub

Or, if each slide only has 1 content placeholder, this checks that the first part of the name is correct, then resizes:

Sub resizeImage()
    Dim sld As Slide
    Dim shp As Shape

    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If Left(shp.Name, 19) = "Content Placeholder" Then
                With shp
                    .Height = 400
                    .Width = 300
                    .Left = 45
                    .Top = 45
                End With
            End If
        Next sld
    Next shp
End Sub

Upvotes: 0

Related Questions