View Full Version : Powerpoint Macro has to run twice to work completely?
quickreactor
03-20-2018, 12:45 AM
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
John Wilson
03-20-2018, 02:06 AM
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
quickreactor
03-20-2018, 03:21 AM
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
John Wilson
03-20-2018, 06:49 AM
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.