PowerPoint VBA Series - How to loop through each PPTX's Slide Master(s) and it's related Layouts

Looping through Slide Master Layouts

Summary: what is demonstrated in this example?

  • determine how many, then loop through how many Slide Masters you have
  • determine how many, then loop through each custom layout of each Slide Master
  • determine if any shape is out of bounds of the Slide Master height
    • if the shape meets certain required conditions
      • set the activewindow so the user can see the slide being examined
      • select the shape with your matching conditions, making it visible to the use later
      • call a subroutine/function to act on your shape
        • pass variables to a subroutine
        • use a case statement that evaluates your shape.type
        • use a msgbox to ask to user what do do with the matching shape

If you need to use VBA to update your Slide Master and it's Layouts

  • First, there may be more than one Slide Master, and each SM will likely have multiple layouts.
    • modify the SM, then loop through each of it's layouts to modify them as well
    • repeat for each SM, if more than one exists

In the following example we will :

  • detect how many slide masters there are in your pptx
    • loop through each layout in each slide master
      • then we can do what we need to do on each SM and it's Layouts

In this example, on each SM and Layout it will:

  • loop through all shapes, determine if they are shape.type group
  • if the group is out of bounds and is the exact size of the shape.type = group we want to detect
    • it shows you the group for final validation and asks you if you want to delete it

Option Explicit

Sub ColorGroupCheck()
    Dim shp As Shape
    Dim sld As Slide
    Dim oMaster As Design
    Dim oLayout As CustomLayout
    Dim sldHeight As Long
    Dim userResponse As Integer
    'Debug.Print ActivePresentation.PageSetup.SlideHeight
    'Debug.Print ActivePresentation.Designs.Count
    sldHeight = ActivePresentation.SlideMaster.Height
    
    For Each oMaster In ActivePresentation.Designs
        'Debug.Print oMaster.SlideMaster.CustomLayouts.Count
        ActiveWindow.ViewType = ppViewMasterThumbnails
        
        For Each shp In oMaster.SlideMaster.Shapes
            shp.Select
            CheckFor_ColorGroup shp
        Next shp
        
        For Each oLayout In ActivePresentation.SlideMaster.CustomLayouts
            ActiveWindow.ViewType = ppViewMasterThumbnails
                oLayout.Select
                For Each shp In oLayout.Shapes
                    shp.Select
                    CheckFor_ColorGroup shp
                Next shp
        Next oLayout
    Next oMaster

End Sub

Sub CheckFor_ColorGroup(myShp)
    Dim myUserResponse As Integer
    Select Case myShp.Type
        Case msoGroup
            If myShp.Top < -5 And myShp.Width > 235 And myShp.Width < 236 Then
                myUserResponse = MsgBox("grp outside top = " & myShp.Top, vbYesNo, "Review selected Group, do you want to delete it?")
            End If
        
            Select Case myUserResponse
            Case 6
                ' yes button
                myShp.Delete
            Case 7
                ' no button, do nothing
            End Select
    End Select
End Sub

Understanding the PPTX View's related to this example:

Normal View

Slide Master "1" Selected

Slide Master "1" Layout "1" Selected - (with an out of bounds shape)


I am not a VBA expert, so if you have better ways to do this, please comment and I may update this example with better coding practices.

Comments

Popular posts from this blog

NET::ERR_CERT_INVALID Issues Using AirWave or NetEDIT with Chrome