title: VBA PowerPoint Slides set custom layout to refresh the layout
body: <p>I have created a script processing many slides and at the end, some slides seem to have glitches in their layout. For example, slide numbers have moved on some slides but not on others. It can be fixed manually by re-assigned the custom layout to the slide.</p>
  <p>How can I do this automatically?</p>
  <p>I could just loop over all slides, find out it's custom layout and re-assign it. But how? This code seems to loop infinitely:</p>
  <pre><code>Dim sld As Slide
  Dim layoutName As String
  Dim layoutIndex As Integer
  
         Set sld = Application.ActiveWindow.View.Slide 
         layoutName = sld.CustomLayout.Name
         layoutIndex = getLayoutIndexByName(layoutName)
         ActivePresentation.Slides(y).CustomLayout = ActivePresentation.Designs(y).SlideMaster.CustomLayouts(layoutIndex) 
  
  
  Function getLayoutIndexByName(xName As String) As Integer
     ActivePresentation.Designs(1).SlideMaster.CustomLayouts.Item (1)
     With ActivePresentation.Designs(1).SlideMaster.CustomLayouts
        For i = 1 To .Count
          Debug.Print (&quot;inLoop Name: &quot; + .Item(i).Name)
          If .Item(i).Name = xName Then
          getLayoutIndexByName = i
          Exit Function
          End If
     Next
     End With
  
  End Function
  </code></pre>