Consulting

Results 1 to 4 of 4

Thread: Powerpoint Macro has to run twice to work completely?

  1. #1

    Powerpoint Macro has to run twice to work completely?

    I have this PPT vba script to delete all shapes in a region at the bottom left corner of each slide. It works but sometimes it leaves one shape in the area. If I run it again it will get rid of it, but I don't want to have to run it twice! How can I fix this?
    Sub GoAwayDumbText()
     Dim oPres As Presentation
     Dim oSlides As Slides
     Dim oSld As Slide
     Dim oShp As Shape
     Dim PathSep AsString
     Dim sTempString AsString
    
    #If Mac Then
    PathSep =":"
    #Else
    PathSep =""
    #EndIf
    
    Set oPres = ActivePresentation
    Set oSlides = oPres.Slides
    
    ForEach oSld In oSlides
    ForEach oShp In oSld.Shapes
        If oShp.Left <=135And oShp.Top >=260Then
        oShp.Delete
        Else
        EndIf
    Next oShp
    
    Next oSld
    
    EndSub

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Try

    Sub GoAwayDumbText()
     Dim oPres As Presentation
     Dim oSlides As Slides
     Dim oSld As Slide
     Dim oShp As Shape
     Dim PathSep As String
     Dim sTempString As String
     Dim L As Long
    
    
    ' Not sure why you think you need this
    ' In any case it is incorrect
    ' The PC PathSep is "\"
    '#If Mac Then
    'PathSep = ":"
    '#Else
    'PathSep = ""
    '#End If
    ''''''''''''''''''''''''''''
    
    
    Set oPres = ActivePresentation
    Set oSlides = oPres.Slides
    
    
    For Each oSld In oSlides
    ' Always work backwards when deleting shapes
    For L = oSld.Shapes.Count To 1 Step -1
    Set oShp = oSld.Shapes(L)
        If oShp.Left <= 135 And oShp.Top >= 260 Then
        oShp.Delete
        Else
        End If
    Next L
    Next oSld
    
    
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    Thank you John! It works perfectly.

    I'm quite new to VBA and based this code off someone elses script where they had that PC Mac stuff and I was too scared too delete it. Now I can get rid of it.

    I appreciate the comments. I will be able to put this into practice soon as I have several other macros in mind working with shapes.

    All the best to you
    Barnaby

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    It may not be obvious why you loop backwards.
    Imaging you have shapes 1,2 and 3 and only 1 and 2 meet the delete criteria.
    In a normal loop the code looks at shape 1 and deletes it.

    Next it looks for shape 2

    The problem is because you deleted shape 1 the old shape 3 is now shape 2 and it doesn't need deleting. The original shape 2 has been skipped over.
    Work through the same logic but starting with shape 3 and working back.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

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