简体   繁体   English

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

[英]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:因此,我使用以下代码将文本框添加到几张幻灯片的 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

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.所以我将使用相同的方法为其他幻灯片添加不同的标题。

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

You can go through all the slides with numbers from the array you set:您可以通过所有幻灯片 go 使用您设置的数组中的数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM