I have two sections in a PowerPoint presentation and want to add a new slide to the end of each section. My code works for the first section but for the second section the new slide is placed at the end of the first section.
Any help how to find a solution for this is much appreciated!
Sub AddSlidesAtEndOfSection()
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Slide
Dim sldCount As Integer
Dim SecNum As Integer
'Create an Instance of PowerPoint
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle PowerPoint Application not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
If PowerPointApp.Presentations.Count = 0 Then
Set myPresentation = PowerPointApp.Presentations.Add
With myPresentation
.PageSetup.SlideWidth = 8.5 * 72
.PageSetup.SlideHeight = 11 * 72
.SectionProperties.AddSection 1, "section one"
.SectionProperties.AddSection 2, "section two"
End With
Else
Set myPresentation = PowerPointApp.ActivePresentation
End If
'-------------> Add Slide at end of each section <-------------
For SecNum = 1 To 2
sldCount = myPresentation.SectionProperties.SlidesCount(SecNum)
'add slide
Set mySlide = myPresentation.Slides.Add(sldCount + 1, ppLayoutBlank)
mySlide.MoveToSectionStart (SecNum)
With myPresentation.SectionProperties
SlideCount = .SlidesCount(SecNum)
FirstSecSlide = .FirstSlide(SecNum)
mySlide.MoveTo toPos:=FirstSecSlide + SlideCount - 1
End With
Next
End Sub