Positioning images on slides in PowerPoint 2007 using vba

15,136

It's unlikely that MS will phase out VBA any time soon. Too many large corporate clients would roast them if they did. All the same, you're right, the documentation is bad and getting worse with every release.

Makes places like this all the more valuable, then. So, a little cleanup to your basic loop first:

Sub SlideLoop()
    Dim osld As Slide

    For Each osld In ActivePresentation.Slides
        ' osld.Select  No need to select a slide before acting on it
        'MsgBox "The slide index of the current slide is: " & _
        '     ActiveWindow.View.Slide.SlideIndex
        MsgBox "The slide index of the current slide is: " & cstr(osld.SlideIndex)
    Next osld

End Sub

So here's a start on how to do what you're after:

Sub SlideLoop()
    Dim osld As Slide
    Dim oSh As Shape

    For Each osld In ActivePresentation.Slides
        ' check each shape on the slide
        ' is it an image or whatever you're looking for?
        For Each oSh In osld.Shapes
            With oSh
                If .Type = msoLinkedPicture _
                Or .Type = msoPicture Then

                ' position it to taste
                .Left = 100
                .Top = 100

                ' centering/resizing gets trickier
                ' but is still possible.
                ' Exercise for the reader?
                ' Hint:
                ' ActivePresentation.PageSetup.SlideWidth and .SlideHeight
                ' tells you the width and height of the slide
                '
                ' All values are in Points (72 to the inch)

                End If
            End With
        Next    ' Shape
    Next osld   ' Slide

End Sub
Share:
15,136
jedmatic
Author by

jedmatic

Updated on June 04, 2022

Comments

  • jedmatic
    jedmatic almost 2 years

    I'd like to do one of two things in PowerPoint 2007 on Windows.

    The first is to change the default location for pasting in an image. When I paste in a graph I made with SAS, it pastes into the upper lefthand corner. Ideally, I'd like to change the default paste position. There don't seem to be any simple options for this but I thought maybe it was possible with VBA.

    If it's not possible, then I'd like to write a VBA macro to step through each slide and change the image position.

    I got a slide loop to work, thanks to this and other sites (the MsgBox is just a test):

    Sub SlideLoop()
        Dim osld As Slide
    
        For Each osld In ActivePresentation.Slides
            osld.Select
            MsgBox "The slide index of the current slide is: " & _
                 ActiveWindow.View.Slide.SlideIndex
        Next osld
    
    End Sub
    

    Beyond that, I haven't had much luck. I have seen code snippets that select all images on a slide and crop or resize them, and I found this bit on excelhelphq.com that is meant to position an image:

    With ActiveWindow.Selection.ShapeRange
      .Left = 50 'change the number for desired x position
      .Top = 50 'change the number for desired y position
    End With 
    

    But I'm not sure how to integrate it into the loop, and the online documentation for Powerpoint VBA is not particularly robust. Some code deals with the ShapeIndex but I wasn't sure how to work with that.

    I should mention that I have only one image on a slide when I have an image (some slides do not have images at all, though).

    This seems like the best time-saving approach, though I'm still manually pasting into PowerPoint in the first place.

    I appreciate any help with this! I couldn't find anything that addressed this exact question.

    Is VBA for PPT being phased out? It feels like Microsoft doesn't want people to be able to figure out how to use it based on their not-stellar online documentation.

  • jedmatic
    jedmatic over 9 years
    Thanks so much Steve! I took out the LinkedPicture check for simplicity but otherwise this worked perfectly.