Search code examples
vbapowerpoint

Formatting images without select


I'm want to perform a variety of formatting options on images in slides.

The macro runs on images that I've SELECTED in a slide, but I'd like to run the macro without selecting the images.

Here's how I'm currently manipulating images (in this case aligning the image to the horizontal center of the slide) and the piece of code that I'm looking for help replacing:

With ActiveWindow.Selection.ShapeRange
 .Align (msoAlignCenters), msoTrue
End With

Here's the entire code body so far:

Sub TestCenterImage()
Dim osld As Slide
Dim oshp As Shape

For Each osld In ActivePresentation.Slides
 If osld.SlideIndex > 1 Then Exit Sub 'I don't know if I need this line
 For Each oshp In osld.Shapes
  If CheckIsPic(oshp) = True Then 'Making sure that we're only working with images
   With ActiveWindow.Selection.ShapeRange 'The portion of code I need help with
    .Align (msoAlignCenters), msoTrue
   End With
  End If
 Next oshp
Next osld
End Sub

Function CheckIsPic(oshp As Shape) As Boolean
If oshp.Type = msoPicture Then CheckIsPic = True
 If oshp.Type = msoPlaceholder Then
  If oshp.PlaceholderFormat.ContainedType = msoPicture Then CheckIsPic = True
End If
End Function

Solution

  • Try it this way instead:

    Sub TestCenterImage()
    Dim osld As Slide
    Dim oShp As Shape
    
    For Each osld In ActivePresentation.Slides
     'If osld.SlideIndex > 1 Then Exit Sub 'I don't know if I need this line
     For Each oShp In osld.Shapes
      If CheckIsPic(oShp) = True Then 'Making sure that we're only working with images
        CenterOnSlide oShp
       'End With
      End If
     Next oShp
    Next osld
    End Sub
    
    Function CheckIsPic(oShp As Shape) As Boolean
    If oShp.Type = msoPicture Then CheckIsPic = True
     If oShp.Type = msoPlaceholder Then
      If oShp.PlaceholderFormat.ContainedType = msoPicture Then CheckIsPic = True
    End If
    End Function
    
    Sub CenterOnSlide(oShp As Shape)
        Dim sngSlideWidth As Single
        Dim sngSlideHeight As Single
    
        sngSlideWidth = ActivePresentation.PageSetup.SlideWidth
        sngSlideHeight = ActivePresentation.PageSetup.SlideHeight
    
        oShp.Left = sngSlideWidth / 2 - oShp.Width / 2
        oShp.Top = sngSlideHeight / 2 - oShp.Height / 2
    
    
    End Sub