从当前幻灯片或所有幻灯片的文本框和形状中删除文本

Delete Text from TextBoxes and Shapes in Current Slide or All Slides

我想 'Delete Texts' 来自所有幻灯片或当前幻灯片,具体取决于我的选择。

我有一些文本框需要一次性全部清除,我还想选择是从所有幻灯片中删除还是只从当前幻灯片中删除。

我收到错误

"Object doesn't support this property or method"

For Each sh In Application.ActiveWindow.View.Slide

我从微软复制了一些代码。

Sub ClearAllTextBox()
    Dim sh          As Shape
    Dim sld         As Slide
    Dim SldDelType  As Boolean
    
    SldDelType = False
    
    Select Case MsgBox("Delete Texts From All Slides?", vbExclamation + vbYesNoCancel)
        Case vbYes:
            SldDelType = True
        Case vbNo:
            SldDelType = False
        Case vbCancel:
            Exit Sub
    End Select
    
    Select Case MsgBox("Are you Sure you want To Delete " & vbNewLine & "all Text from all Shapes/TextBoxes?", vbExclamation + vbYesNo)
        Case vbNo:
            Exit Sub
        Case vbYes:
            If SldDelType Then
                For Each sld In ActivePresentation.Slides
                    For Each sh In sld.Shapes
                        If sh.HasTextFrame Then
                            sh.TextFrame.DeleteText
                        End If
                    Next sh
                Next sld
            Else:
                For Each sh In Application.ActiveWindow.View.Slide
                    If sh.HasTextFrame Then
                        sh.TextFrame.DeleteText
                    End If
                Next sh
            End If
    End Select
End Sub

如果你想遍历幻灯片的所有形状,你需要这样说。你已经在上半部分做对了,在那里你写 For Each sh In sld.Shapes。如果你想遍历一张幻灯片的所有形状,你需要做同样的事情:

    (...)
Else
    For Each sh In Application.ActiveWindow.View.Slide.Shapes
        If sh.HasTextFrame Then
        (...)
        End If
    Next sh
End If

或者使用幻灯片变量来拆分那条长语句:

Else
    Set sl = Application.ActiveWindow.View.Slide
    For Each sh sl.Shapes
        (...)
    Next sh
End If