【问题标题】:TextBox Added to Each Slide Actually Added Many to Each Slide添加到每张幻灯片的文本框实际上添加了很多到每张幻灯片
【发布时间】:2020-07-03 06:39:30
【问题描述】:

我是 vba 的新手,需要一些指导。我正在尝试为每张幻灯片(在幻灯片区域之外)添加一个文本框,以便快速查看幻灯片的 ID 和索引。我拼凑了几个位来创建下面的代码。它可以工作,但是添加到每张幻灯片的文本框数量是演示文稿中的幻灯片数量(112)而不是 1。我还想知道如何调整它以在更改幻灯片时进行刷新。

提前致谢!

代码如下:

Sub AddSlideInfo()

'Original Source: http://www.pptfaq.com/FAQ01180-Add-presentation-file-name-to-each-slide-master.htm

Dim x As Long
Dim oSh As Shape
Dim oSl As Slide

With ActivePresentation

    On Error Resume Next 'In case the shape does not exist.

    ' On each slide in the presentation:
    For x = 1 To .Slides.Count

    Set oSl = ActivePresentation.Slides(x)

        ' Create a textbox at 0" from left,
        ' -120.24 points from top of slide ( -1.67") from top left corner
        ' Make it 90 points high, 300 points wide 1.25" x 5.5"
        ' Change any of these numbers at will

        For Each oSl In ActivePresentation.Slides

            With oSl

                Set oSh = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=-120, Width:=300, Height:=90)

                ' Give it a name so we can find it later
                oSh.Name = "SlideInfo"

                'Add some formatting and dummy text
                With oSh.TextFrame.TextRange

                    .Font.Name = "Berlin Sans Demi"
                    .Font.Size = 12
                    .Text = _
                    "Slide Info: " & vbNewLine & _
                    "Slide Index: " & oSl.SlideIndex & vbNewLine & _
                    "Slide ID: " & oSl.SlideID 


                End With

            End With

       Next

    Next x

End With

End Sub  

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

    您的代码使用 For x = 1 To .Slides.Count 遍历所有幻灯片,然后使用 For Each oSl In ActivePresentation.Slides 再次遍历所有幻灯片.两者都不需要。

    以下是您的代码的简化版本。它只在幻灯片中循环一次。如果 SlideInfo 文本框存在,它会删除它(使用 On Error Resume Next 来捕获错误)......但您可以稍后清理它:)......然后每次都干净地重新创建文本框。

    Option Explicit
    
    Sub AddSlideInfo()
    
        Const cShapeName = "SlideInfo"
        Dim oSh As Shape
        Dim oSl As Slide
    
        On Error Resume Next
    
        With ActivePresentation
            For Each oSl In ActivePresentation.Slides
                With oSl
    
                    .Shapes(cShapeName).Delete
                    Set oSh = .Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=-120, Width:=300, Height:=90)
                    oSh.Name = cShapeName
    
                    'Add some formatting and dummy text
                    With oSh.TextFrame.TextRange
                            .Font.Name = "Berlin Sans Demi"
                            .Font.Size = 12
                            .Text = _
                            "Slide Info: " & vbNewLine & _
                            "Slide Index: " & oSl.SlideIndex & vbNewLine & _
                            "Slide ID: " & oSl.SlideID
                    End With
                End With
            Next
        End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多