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 ExplicitSub ColorGroupCheck()Dim shp As ShapeDim sld As SlideDim oMaster As DesignDim oLayout As CustomLayoutDim sldHeight As LongDim userResponse As Integer'Debug.Print ActivePresentation.PageSetup.SlideHeight'Debug.Print ActivePresentation.Designs.CountsldHeight = ActivePresentation.SlideMaster.HeightFor Each oMaster In ActivePresentation.Designs'Debug.Print oMaster.SlideMaster.CustomLayouts.CountActiveWindow.ViewType = ppViewMasterThumbnailsFor Each shp In oMaster.SlideMaster.Shapesshp.SelectCheckFor_ColorGroup shpNext shpFor Each oLayout In ActivePresentation.SlideMaster.CustomLayoutsActiveWindow.ViewType = ppViewMasterThumbnailsoLayout.SelectFor Each shp In oLayout.Shapesshp.SelectCheckFor_ColorGroup shpNext shpNext oLayoutNext oMasterEnd SubSub CheckFor_ColorGroup(myShp)Dim myUserResponse As IntegerSelect Case myShp.TypeCase msoGroupIf myShp.Top < -5 And myShp.Width > 235 And myShp.Width < 236 ThenmyUserResponse = MsgBox("grp outside top = " & myShp.Top, vbYesNo, "Review selected Group, do you want to delete it?")End IfSelect Case myUserResponseCase 6' yes buttonmyShp.DeleteCase 7' no button, do nothingEnd SelectEnd SelectEnd 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.
I used the following sources as references:
Comments
Post a Comment