如何获取章节标题并更改该章节中幻灯片的标题?

How do I grab the section title and change the title of the slides in the section?

我正在 VBA 中编写脚本以将幻灯片的标题更改为章节标题。我有多个部分,我想遍历 ppt 以更改幻灯片的所有标题,使其具有与其部分相同的部分标题。

我试过如何获取章节标题并将其设置为幻灯片的标题。

Sub test()

ActivePresentation.Slides.Name = ActivePresentation.SectionProperties(sectionName)


End Sub

我需要添加迭代,我需要我相信语法很乱。

希望您正在寻找类似于以下内容的内容。代码

  1. 遍历 ActivePresentation 中的每张幻灯片,如果没有标题则添加一个。
  2. 通过获取 sectionIndex property of the slide, and then using that index in the SectionProperties.Name 方法检索相应文本来更改标题文本。

Sub ChangeMyTitles()
    Dim sld As Slide
    Dim titleShape As Shape

    If ActivePresentation.SectionProperties.Count = 0 Then Exit Sub

    For Each sld In ActivePresentation.Slides
        With sld
            If Not .Shapes.HasTitle Then
                Set titleShape = .Shapes.AddTitle
            Else
                Set titleShape = .Shapes.Title
            End If

            titleShape.TextFrame2.TextRange.Text = ActivePresentation.SectionProperties.Name(.sectionIndex)
        End With
    Next sld
End Sub

编辑:

如果您想修改与标题不同的占位符,您可以这样做。根据您的屏幕截图,我假设您要修改的占位符是第 3 个(标题是第 1 个,body 是第 2 个,章节是第 3 个),但您可能需要更改下面的 3 .

Sub ChangeMyChapters()
    Dim sld As Slide
    Dim chapterShape As Shape

    If ActivePresentation.SectionProperties.Count = 0 Then Exit Sub

    For Each sld In ActivePresentation.Slides
        With sld
            Set chapterShape = .Shapes.Placeholders(3)
            chapterShape.TextFrame2.TextRange.Text = ActivePresentation.SectionProperties.Name(.sectionIndex)
        End With
    Next sld
End Sub