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

ppw

有点问题,我有一些VBA代码可以遍历我ppt中的所有工作表,可以遍历每个ppt中的所有形状,如果找不到特定的文本字符串,则可以删除ppt。除了代码似乎无缘无故地停止循环外,它似乎还可以正常工作。我必须按F5大约4次才能使代码循环遍历所有工作表。这可能与我的代码有关,所以我认为我应该首先尝试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

我的ppt中有34张幻灯片,每次运行将循环遍历大约7张幻灯片,正确地识别并删除我不需要的幻灯片,但是如果没有任何错误,它将停止循环并继续执行其余代码。如果有所不同,可以在幻灯片17和18上找到该字符串。我添加了一些额外的内容来尝试解决debug.prints和i = 0之类的问题,但我只是无法弄清楚自己在做什么错。

提前谢谢了!

ppw

杰米·加洛奇(Jamie Garroch)-MVP

每当遍历该集合中的每个对象时删除该集合中的任何对象时,都需要向后计数。因此,在这些情况下,您不能在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

http://youpresent.co.uk下载更多免费的PowerPoint宏和加载项。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Powerpoint VBA 从当前幻灯片或所有幻灯片中的文本框和形状中删除文本

VBA Powerpoint:循环并重命名所有幻灯片

PowerPoint VBA-循环播放所有幻灯片,所有形状,查找图表,将datalabel颜色设置为Black

VBA 不循环任何 PowerPoint 幻灯片

PowerPoint VBA选择幻灯片

从Access VBA填充Powerpoint幻灯片

在PowerPoint中使用VBA删除一张幻灯片上的所有图像

位于 PowerPoint 的所有幻灯片放映窗口前面的 VBA 用户窗体

Powerpoint VBA可以选择幻灯片特定区域内的所有形状

Excel 到 PowerPoint VBA 循环

Powerpoint幻灯片显示不在幻灯片中的页面

使用VBA将Excel范围粘贴到Powerpoint模板的某些幻灯片中

VBA代码可更改Powerpoint每张幻灯片中的特定单词

Powerpoint vba 锁定所有形状

PowerPoint VBA 删除所有评论

PowerPoint VBA导出某些幻灯片到pdf

Excel VBA在PowerPoint中复制幻灯片

如何使用VBA动态引用PowerPoint幻灯片

使用vba在PowerPoint 2016中插入幻灯片缩放

PowerPoint幻灯片打开时如何运行VBA代码

VBA 中的 Powerpoint 幻灯片计数变量

从Access VBA打开PowerPoint演示文稿的特定幻灯片

PowerPoint VBA创建和保存幻灯片

PowerPoint VBA代码将焦点重新带到正在运行的PowerPoint幻灯片放映中

Powerpoint VBA - 如何修改代码以正确循环?

带有多个按钮的 Powerpoint VBA 插件

使用VBA获取Powerpoint幻灯片中同一对象两次单击之间的经过时间

Powerpoint VBA将带有关键字的幻灯片另存为JPEG

使用VBA在excel中的每张幻灯片上创建带有多个图表的Powerpoint