从 Excel 文件在 Powerpoint 中添加幻灯片标题

Adding slide titles in Powerpoint from an Excel file

我想在 excel 文件的 ppt 中添加幻灯片标题。但我不能把它放在一个循环中。从 A1 到 A7 的每个单元格在文件中都有幻灯片标题,因此我想根据幻灯片编号将标题放在幻灯片中。 下面是我写的代码。

For i = 1 To 7
With Application.Presentations(1)

    For Each ppSlide In .Slides()
       If ppSlide.Shapes.HasTitle Then ppSlide.Shapes.Title.TextFrame.TextRange = xlWorkSheet.Cells(i, 1)
    Next ppSlide

End With

Next i

我假设你 运行 VBA-code 使用 PowerPoint。

如果找到包含标题的相应形状,则只能设置幻灯片标题。这个形状可以通过它的Shape.Type = msoPlaceholderPlaceholderFormat.Type = ppPlaceholderTitle(或PlaceholderFormat.Type = ppPlaceholderCenterTitle)来识别。

如果您想从 1 循环到 7,那么您也可以通过从 1 到 7 的索引来处理幻灯片(而不是通过对所有幻灯片进行额外的 For Each 循环)。

Private Sub SetTitlesFromExcellist()
    Dim xlApp As Excel.Application
    Dim xlWorkSheet As Excel.worksheet
    Dim ppSlide As PowerPoint.Slide
    Dim ppShape As PowerPoint.Shape
    Dim i As Long

    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    On Error GoTo 0
    If xlApp Is Nothing Then Exit Sub
    Set xlWorkSheet = xlApp.ActiveSheet

    For i = 1 To 7
        Set ppSlide = ActivePresentation.Slides(i)
        If ppSlide.Shapes.HasTitle Then
            For Each ppShape In ppSlide.Shapes
                If ppShape.Type = msoPlaceholder Then
                    Select Case ppShape.PlaceholderFormat.Type
                    Case ppPlaceholderCenterTitle, ppPlaceholderTitle
                        ppShape.TextFrame.TextRange.Text = xlWorkSheet.Cells(i, 1)
                        Exit For
                    End Select
                End If
            Next ppShape
        End If
    Next i
End Sub