首页 > 解决方案 > 将幻灯片添加到部分

问题描述

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

目前它在最后一张下方的两张幻灯片,但由于幻灯片的数量将链接我无法使用 -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

标签: vbapowerpoint

解决方案


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

在您的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

由于您需要“索引”部分的部分编号,因此我编写了一个快速函数来返回该编号:

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

推荐阅读