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