Powerpoint VBA - 如何将文本框添加到多张幻灯片

Powerpoint VBA - How to add text box to multiple slides

所以我使用以下代码将文本框添加到几张幻灯片的 header 中:

Set myDocument = ActivePresentation.Slides.Range(Array(4, 5, 6))
Set newTextBox = myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
    260, Top:=30, Width:=541.44, Height:=43.218)
    With newTextBox.TextFrame.TextRange
        .Text = "Test Text"
        .Font.Size = 17
        .Font.Name = "Arial"
End With

当我 运行 此代码时,我收到一个自动化错误并且它不起作用。如果我在一张幻灯片上做它确实有效。有谁知道为什么?我想要做的是将 headers 添加到特定的幻灯片。所以我将使用相同的方法将不同的 header 添加到其他幻灯片。

幻灯片没有 headers。但这里是有效的代码:

Sub AddTextBoxes()
    Dim oSlide As Slide
    Dim oShape As Shape

    For Each oSlide In ActivePresentation.Slides
        If oSlide.SlideIndex = 4 Or oSlide.SlideIndex = 5 Or oSlide.SlideIndex = 6 Then
            Set oShape = oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=260, Top:=30, Width:=541.44, Height:=43.218)
            With oShape.TextFrame.TextRange
                .Text = "Test Text"
                .Font.Size = 17
                .Font.Name = "Arial"
            End With
        End If
    Next oSlide
End Sub

您可以使用您设置的数组中的数字浏览所有幻灯片:

Sub slideTextBoxes()
    For Each myDocument In ActivePresentation.Slides.Range(Array(4, 5, 6))
        Set newTextBox = myDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, _
            260, Top:=30, Width:=541.44, Height:=43.218)
        With newTextBox.TextFrame.TextRange
            .Text = "Test Text"
            .Font.Size = 17
            .Font.Name = "Arial"
        End With
    Next
End Sub