hockeyhorror
hockeyhorror

Reputation: 57

Powerpoint VBA - How to add text box to multiple slides

So I'm using the following code to add a text box to the header of several slides:

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

When I run this code I get an automation error and it doesn't work. If I do it on a single slide it does work. Does anyone know why? What I'm attempting to do is add headers to specific slides. So I will be using the same method to add different headers to other slides as well.

Upvotes: 1

Views: 1396

Answers (2)

Алексей Р
Алексей Р

Reputation: 7627

You can go through all the slides with numbers from the array you set:

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

Upvotes: 1

John Korchok
John Korchok

Reputation: 4913

Slides don't have headers. But here is code that will work:

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

Upvotes: 1

Related Questions