有點問題,我有一些VBA代碼循環遍歷所有在我的PPT中的表,循環遍歷每個ppt中的所有形狀,並刪除了ppt,如果一個特定的字符串沒有找到文字。除了代碼似乎無緣無故停止循環以外,它似乎完美工作。我必須打F5約4次才能讓代碼遍歷所有表單。這可能與我的代碼有關,所以我想我會先嚐試Stackoverflow的好人。Powerpoint VBA循環沒有循環遍歷所有幻燈片
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
謝謝@JamieG的解釋和固定代碼。 – ppw