Search code examples
vbapowerpoint

How to remove textbox on each slide?


I want to remove the page number text box in each slide, created by a 10 years old version of MS PowerPoint, in the following format.

page 1 of 47

First attempt:

With ActivePresentation.Slides.Find
    .Forward = True
    .Wrap = wdFindStop
    .Text = "*/47"
    .Replacement.Text = ""
    .Replace = wdReplaceAll
    .MatchCase = False
End With

Second attempt:

Sub ClNumbers()
Dim oSl As Slide
Dim oSh As Shape
Dim oTxtRng As TextRange
Dim sTextToFind As String

sTextToFind = "*/47"

For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
        If oSh.HasTextFrame Then
            If oSh.TextFrame.HasText Then
                If InStr(oSh.TextFrame.TextRange.Text, sTextToFind) > 0 Then
                    Set oTxtRng = oSh.TextFrame.TextRange.Characters(InStr(oSh.TextFrame.TextRange.Text, sTextToFind), Len(sTextToFind))
                    Debug.Print oTxtRng.Text
                    With oTxtRng
                        .Font.Bold = True
                    End With
                End If
            End If
        End If
    Next
Next
End Sub

How do I remove all page numbers by VBA?


Solution

  • (1) As far as I know, there is no global find/replace in VBA for Powerpoint. At least there is no Find-method for the Application-object, the Presentation-Object or the Slides- or Shapes collection. Your attempt 1 fails with a compiler error.

    (2) Powerpoint doesn't support wildcard or regular expressen search.

    (3) In your 2nd attempt, you would mark the text in Bold rather than delete the shape or the text of the shape - if it was found (it isn't).

    You will need to loop over all shapes of all slides and check if it contains a certain text pattern. You 2nd attempt is close, but the VBA function InStr doesn't work with wildcards either. Instead, you could use the VBA Like-operator.

    You now need to make up your mind what you want to do with the shapes:
    o You can delete them completly with oSh.Delete
    o You can hide them with oSh.Visible = False
    o You can just delete the text with oSh.TextFrame.TextRange.Characters.Delete

    (4) If the shapes are defined on the slide master of the presentation, the code will not do anything as the shapes are not present on the slides at all. In that case, simply edit the slide master in Powerpoint - no code needed.

    So your code could look like your 2nd attempt, just modify the inner part (and decide what you want to do)

    If oSh.TextFrame.TextRange.Text Like sTextToFind Then
        ' oSh.Delete
        ' oSh.Visible = False
        oSh.TextFrame.TextRange.Characters.Delete
    End If