1
votes

I am trying to code an automatic agenda/table of contents generator in VBA for PowerPoint, which generates agenda bullet points based on the titles of the sections inside the PowerPoint presentation. Since I also want the agenda to appear at the beginning of every section, I am struggling with the fact that the method

.AddSlide(Index (I am inserting the ID of a section´s first slide here), pCustomLayout )

adds the slide(s) just before the section (so actually at the end of the previous section) because it's just based on an ID and does not say "insert the slide at a section's beginning".

Is there an easy solution (without deleting and recreating the section for instance) to achieve that the slides are created just at the beginning of a section and not at the end of the previous section?


Solution

 Sub moveSlidesToSectionStart(pSectionIndex, pFirst, pLast)
  Dim objPresentation As Presentation
  Set objPresentation = Application.ActivePresentation

  totalSlides = pLast - pFirst + 1
  Dim arr()
  ReDim arr(totalSlides - 1)

  For i = 0 To totalSlides - 1 'fill array with all slides (slide numbers) that need to be moved
    arr(i) = pFirst + i
  Next i

  objPresentation.Slides.Range(arr).MoveToSectionStart(pSectionIndex)

 End Sub
1

1 Answers

1
votes

You could use the MoveToSectionStart method available on the slide. Pass in the section index as the argument and it will place the slide right at the start of that section.

Function MoveSlideToSectionStart(Sld As Slide, SectionIndex As Long) As Boolean

If Sld.Parent.SectionProperties.Count < SectionIndex Then
    MoveToSection = False
    Exit Function
End If

Call Sld.MoveToSectionStart(SectionIndex)
MoveToSection = True
End Function

Sub Test()
Debug.Print MoveToSection(ActivePresentation.Slides(6), 1)
End Sub