将幻灯片添加到部分

Add slide to section

我正在尝试将使用以下代码创建的幻灯片添加到名为 "Index" 的特定部分。

目前它比最后一张幻灯片低两张,但由于幻灯片的数量将被链接,我无法使用 -2 函数进行引用,所以我不得不求助于部分引用。

Public Function GetLayout( _
    LayoutName As String, _
    Optional ParentPresentation As Presentation = Nothing) As CustomLayout

    If ParentPresentation Is Nothing Then
        Set ParentPresentation = ActivePresentation
    End If

    Dim oLayout As CustomLayout
    For Each oLayout In ParentPresentation.SlideMaster.CustomLayouts
        If oLayout.Name = LayoutName Then
            Set GetLayout = oLayout
            Exit For
        End If
    Next
End Function

Sub AddCustomSlide()
    Dim oSlides As Slides, oSlide As Slide
    Set oSlides = ActivePresentation.Slides
    Set oSlide = oSlides.AddSlide(oSlides.Count - 2, GetLayout("Processwindow"))
End Sub

解决方法是使用Slide对象的.MoveToSectionStart方法。

在您的 AddCustomSlide 例程中,添加一行:

Sub AddCustomSlide()
    Dim oSlides As Slides, oSlide As Slide
    Set oSlides = ActivePresentation.Slides
    Set oSlide = oSlides.AddSlide(oSlides.Count - 2, GetLayout("Title Only"))
    oSlide.MoveToSectionStart GetSectionNumber("Index")
End Sub

由于您需要 "Index" 节的节号,我已经为 return 该节号编写了一个快速函数:

Private Function GetSectionNumber( _
        ByVal sectionName As String, _
        Optional ParentPresentation As Presentation = Nothing) As Long

    If ParentPresentation Is Nothing Then
        Set ParentPresentation = ActivePresentation
    End If

    GetSectionNumber = -1
    With ParentPresentation.SectionProperties
        Dim i As Long
        For i = 1 To .Count
            If .Name(i) = sectionName Then
                GetSectionNumber = i
                Exit Function
            End If
        Next i
    End With
End Function