Consulting

Results 1 to 3 of 3

Thread: Code works, but error when there is a picture on the slide

  1. #1

    Question Code works, but error when there is a picture on the slide

    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

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    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
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Works fine now,

    Thanks for helping me out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •