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

时间:2019-06-14 11:03:04

标签: vba vb.net powerpoint-vba

我正在尝试从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

1 个答案:

答案 0 :(得分:0)

我假设您在PowerPoint中运行VBA代码。

如果找到包含标题的对应图形,则只能设置幻灯片标题。此形状可以通过其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