Search code examples
vbapowerpoint

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


I want to 'Delete Texts' from All Slides or Current Slide depending on my choice.

I have textboxes that I needed to clear all at once and I also want to choose whether to Delete from all Slides or just this Current Slide.

I getting the error

"Object doesn't support this property or method"

on For Each sh In Application.ActiveWindow.View.Slide

I copied some of the code from Microsoft.

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

Solution

  • If you want to loop over all shapes of a slide, you need to say so. You are doing it right already in the upper part, there you write For Each sh In sld.Shapes. You need to do the same if you want to loop over all shapes of a single slide:

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

    or use a slide variable to split that long statement:

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