PDA

View Full Version : [SOLVED:] Code works, but error when there is a picture on the slide



Test.Testman
08-03-2016, 07:41 AM
I want to delete a Text Box with a certain Text in PowerPoint 2010 and encountered a weird problem.

The code works fine if the slide has NO PICTURES on it. However, if there is a picture on the slide im getting an error message:

"Runtime error -2147024809 (80070057)
Given Value is out of bounds" (translated so might not be exact)

Here is my code:


Sub delete()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and has text (...)
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
SlideToCheck.Shapes(ShapeIndex).TextFrame.TextRange.Text = ("sample text") Then
' Delete the shape
SlideToCheck.Shapes(ShapeIndex).TextFrame.DeleteText
End If
Next
Next
End Sub

Like I said, the code works 100% fine without pictures.

Hope someone can help,
Thanks

John Wilson
08-03-2016, 01:37 PM
It's because you have used AND. When it gets to a picture it checks both that it is a textbox AND that the textrange is "sample text"

Pictures cannot have a textrange and it will error

You need to do this:


Sub delete()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and has text (...)
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox Then
If SlideToCheck.Shapes(ShapeIndex).TextFrame.TextRange.Text = ("sample text") Then
' Delete the shape
SlideToCheck.Shapes(ShapeIndex).TextFrame.DeleteText
End If
End If
Next
Next
End Sub

Test.Testman
08-03-2016, 11:16 PM
Works fine now,

Thanks for helping me out!