As title, I want to remove the page number text box in each slide which was created by old version of MS powerpoint 10 years old in following format
page 1 of 47
My 1st attempted code is
With ActivePresentation.Slides.Find
.Forward = True
.Wrap = wdFindStop
.Text = "*/47"
.Replacement.Text = ""
.Replace = wdReplaceAll
.MatchCase = False
End With
My 2nd attempted code is
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
neither does work, would you please help to correct my code to remove all page number by VBA. thanks in advance.
please correct me vba code or provide your elegant method.
CodePudding user response:
(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
CodePudding user response:
Thanks FunThomas, it works perfectly.
Here is the code:
Sub deletepagenumber()
' Remove the page number text box in each slide which was created by old version of MS powerpoint
' Before run this code, please check if the page number could be turned off in the slide master
' Thanks to FunThomas @ stackoverflow
Dim oSl As Slide
Dim oSh As Shape
Dim oTxtRng As TextRange
Dim sTextToFind As String
' Before run this code, please input the format of the page number, e.g. page ?/47, can be searched as "*/47"
sTextToFind = "*/47"
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.Text Like sTextToFind Then
' oSh.Delete
' oSh.Visible = False
oSh.TextFrame.TextRange.Characters.Delete
End If
End If
End If
Next
Next
End Sub