首页 > 解决方案 > VBA代码不适用于删除ppt幻灯片

问题描述

我正在尝试使用特定的关键字删除 ppt 幻灯片。我的代码如下:

Private Sub CommandButton1_Click()
    Dim strFileName As String
    Dim strFolderName As String
    Dim PP As Presentation
    Dim ppMaster
    Dim sText As Variant
    strFolderName = "D:\Shaon_Paul\pptss"
    strFileName = Dir(strFolderName & "\*.pptx*")
    sText = InputBox("Give me some input")


    Do While Len(strFileName) > 0
        Set PP = Presentations.Open(strFolderName & "\" & strFileName)
        Dim oSld As Slide
        Dim oShp As Shape
        Dim L As Long
        For L = ActivePresentation.Slides.Count To 1 Step -1
        Set oSld = ActivePresentation.Slides(L)
             For Each oShp In oSld.Shapes
             On Error Resume Next
                If oShp.HasTextFrame Then
                    If UBound(Split(oShp.TextFrame.TextRange, sText)) > 0 Then
                    PP.Slides(L).Delete
                    End If
                End If
             Next oShp
        Next L

        Set ppMaster = PP.SlideMaster
        With ppMaster
            If UBound(Split(.HeadersFooters.Footer.Text, sText)) > 0 Or UBound(Split(.HeadersFooters.Header.Text, sText)) > 0 Then
                PP.Slides(L).Delete
            End If
        End With

        PP.Save
        PP.Close
        strFileName = Dir
    Loop
End Sub

现在这段代码将打开一个输入框,我必须在其中输入关键字,它将删除我提到路径名的文件夹中可用的 ppt 中的幻灯片。不幸的是,在调试代码时

If UBound(Split(oShp.TextFrame.TextRange, sText)) > 0 Then
                    PP.Slides(L).Delete
                    End If

它会在不执行上面的代码的情况下跳转,并且无法通过给出特定的关键字名称来删除幻灯片。需要帮助来解决此问题。

标签: vbapowerpoint

解决方案


推荐阅读