简体   繁体   English

Powerpoint VBA Loop不在所有幻灯片中循环播放

[英]Powerpoint VBA Loop not looping through all slides

Bit of an issue, I have some VBA code that loops through all of the sheets in my ppt, loops through all of the shapes in each ppt, and deletes the ppt if a specific string of text is not found. 有点问题,我有一些VBA代码可以遍历我ppt中的所有工作表,可以遍历每个ppt中的所有形状,如果找不到特定的文本字符串,则可以删除ppt。 It seems to work perfectly other than the code seems to stop looping for no reason. 除了代码似乎无缘无故地停止循环外,它似乎还可以正常工作。 I have to hit F5 about 4 times for the code to loop through all the sheets. 我必须按F5大约4次才能使代码循环遍历所有工作表。 It could be something to do with my code so I thought I'd try the good people of Stackoverflow first. 这可能与我的代码有关,所以我认为我应该首先尝试Stackoverflow的优秀人才。

Public Sub ExportMBR()
Dim oSld As Slide
Dim oShp As Shape
Dim strSearch As String
Dim i As Integer

strSearch = "R&T MBR"
i = 0

For Each oSld In ActivePresentation.Slides
    Debug.Print (ActivePresentation.Slides.Count)
    Debug.Print (oSld.Name)
    For Each oShp In oSld.Shapes
        If oShp.HasTextFrame Then
            If oShp.TextFrame.TextRange.Find(strSearch) Is Nothing Then
            Else
                Debug.Print (oSld.Name & " Slide found")
                i = i + 1
            End If
        End If
    Next oShp
    If i = 0 Then
        Debug.Print (oSld.Name & " Deleting")
        oSld.Delete
        i = 0
    End If
    i = 0
Next oSld

myQ = "<afilepath>"
myName = myQ & "<anameformat>") & ".pptx"
ActivePresentation.SaveCopyAs myName

Call Shell("explorer.exe " & myQ, vbNormalFocus)

End Sub

There are 34 slides in my ppt, each run will loop through about 7 slides correctly identifying and deleting the slides I do not need, but then without any errors it will just stop looping and continue executing the rest of the code. 我的ppt中有34张幻灯片,每次运行将正确地循环浏览约7张幻灯片,以识别并删除我不需要的幻灯片,但是如果没有任何错误,它将停止循环并继续执行其余代码。 The string is found on slides 17 and 18 if this makes a difference. 如果有区别,可以在幻灯片17和18上找到该字符串。 I have added few bits extra to try and solve the problem like the debug.prints and the i = 0 but I just can't figure out what I'm doing wrong. 我添加了一些额外的内容来尝试解决debug.prints和i = 0之类的问题,但我只是无法弄清楚自己在做什么错。

Many thanks in advance! 提前谢谢了!

ppw w

Whenever you delete any object within a collection as you loop through each object in that collection, you need to count backwards. 每当遍历该集合中的每个对象时删除该集合中的任何对象时,都需要向后计数。 So in these cases you cannot use the For Each oSld In ActivePresentation.Slides statement but do this instead: 因此,在这些情况下,您不能使用ActivePresentation.Slides语句中的For Each oSld,而是这样做:

Dim lCntr as Long
Dim oSld as Slide
For lCntr = ActivePresentation.Slides.Count to 1 Step -1
  Set oSld = ActivePresentation.Slides(lCntr)
  ' Do your stuff here...
  Set oSld = Nothing
Next

Download more free PowerPoint macros and add-ins at http://youpresent.co.uk http://youpresent.co.uk下载更多免费的PowerPoint宏和加载项。

Because Find(strSearch) & oSld.Delete are at the same loop, you need to separate them !! 因为Find(strSearch)oSld.Delete处于同一循环,所以您需要将它们分开! Address the slides which you want to del first and then del them. 先处理要删除的幻灯片,然后再删除它们。

For example: suppose that you have slide_1 & slide_2 & slide_3 and you want to del slide_1 & slide_2 & slide_3. 例如:假设您有slide_1和slide_2和slide_3,并且要删除slide_1和slide_2和slide_3。 Actually, your VBA only del slide_1 & slide_3. 实际上,您的VBA仅删除slide_1和slide_3。

In the loop For Each oSld In ActivePresentation.Slides , the finding sequence should be slide_1 => slide_2 => slide_3. For Each oSld In ActivePresentation.Slides循环中,查找顺序应为slide_1 => slide_2 => slide_3。 However, the first loop cycle will del slide_1, the remaining slides count become 2 (slide_2 & slide_3), so second loop cycle will start from slide_3. 但是,第一个循环周期将删除slide_1,其余的幻灯片数将变为2(sli​​de_2和slide_3),因此第二个循环周期将从slide_3开始。 That's the reason why. 这就是原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM