使用VBA删除不属于命名集的PowerPoint幻灯片设计?

时间:2012-09-25 13:16:58

标签: vba ms-office powerpoint powerpoint-vba office-2010

我有一套自定义布局,这是我认可的企业标准。有一个主要和11个自定义布局。

由于用户可能会粘贴旧内容,因此我知道他们粘贴的任何幻灯片都会带来相应的布局。编写一个按钮的最佳方法是删除任何不属于批准集的自定义布局?

我的代码如下,但它给出了一个错误,上面写着“幻灯片(未知成员):无效请求。无法删除主人。”

感谢任何帮助!

Dim oDesign As design

For Each oDesign In ActivePresentation.Designs

    'if design name is CC standard then
    If oDesign.Name = CCSMNAME$ Then

        Dim oLayout As CustomLayout

        'Check the name of each layout against the permitted set, delete any that are additional
        For Each oLayout In oDesign.SlideMaster.CustomLayouts

            If oLayout.Name <> "Title Slide (Basic)" Or oLayout.Name <> "Title Slide (Standard Stock Image)" Or oLayout.Name <> "Title Slide (Image - Right)" Or oLayout.Name <> "Agenda" Or oLayout.Name <> "Body/Content (Basic)" Or oLayout.Name <> "Report (Approval and Disclaimer)" Or oLayout.Name <> "Report Body/Content" Or oLayout.Name <> "Divider" Or oLayout.Name <> "Quals (Basic -Right)" Or oLayout.Name <> "Quals (Basic - Left)" Or oLayout.Name <> "Content and Closing" Then
                oLayout.Delete
            End If

        Next oLayout

    Else

        'Else, the Design found is not the CC Master so delete it
        '(This runs for all remaining masters)
         oDesign.Delete

    End If

Next oDesign

我现在正在使用以下代码 - 有没有人知道为什么不删除所有其他幻灯片母版,它只删除序列中的下一个然后退出?

Sub CleanupTemplate()

    'Declare some variables
    Dim oDesign As design
    Dim oDesigns As Designs
    Dim oLayout As CustomLayout
    Dim masterCount As Long
    Dim layoutCount As Long
    Dim strInUse As String

    On Error Resume Next

   For Each oDesign In ActivePresentation.Designs

            If oDesign.Name = CCSMNAME$ Then

            MsgBox "The script has found " & oDesign.SlideMaster.CustomLayouts.Count & " layouts in the CC Master. There should be 11 in total. An integrity check will now run to remove any non-approved slide layouts."

                            'Loop through set backwards to keep integrity of data set when deleting
                            For layoutCount = oDesign.SlideMaster.CustomLayouts.Count To 1 Step -1

                                   Set oLayout = oDesign.SlideMaster.CustomLayouts(layoutCount)
                                   Err.Clear

                                   'Check the name of each layout against the permitted set, delete any that are additional
                                   If checkAllowed(oLayout) = False Then
                                    oLayout.Delete
                                   End If

                                   If Err <> 0 Then
                                    strInUse = strInUse & oLayout.Name & " , "
                                   End If

                            Next layoutCount
                            MsgBox ("Any additional layouts deleted, cleanup of CC Master completed.")

             Else

                'Else, a Slide Master has been found that is not the CC Master so delete it
                MsgBox ("An additional Slide Master named " & oDesign.Name & " that is not CC approved has been detected. It is not in use, so it will be removed.")
                oDesign.Delete

             End If

    Next oDesign

    'Alert the user to any foreign slide designs found that couldn't be deleted
    If Len(strInUse) > 0 Then
        MsgBox "The following slide designs seem to be either in use, or protected: " & Left(strInUse, Len(strInUse) - 1)
    End If

End Sub

函数checkAllowed(olay As CustomLayout)As Boolean

    Select Case olay.Name

            Case Is = "Title Slide (Basic)", _
            "Title Slide (Image - Right)", _
            "Title Slide (Standard Stock Image)", _
            "Agenda", "Body/Content (Basic)", _
            "Report (Approval and Disclaimer)", _
            "Report Body/Content", _
            "Divider", _
            "Quals (Basic - Right)", _
            "Quals (Basic - Left)", _
            "Contact and Closing"

                'Return true if any of the above names are a match
                checkAllowed = True

            Case Else

                'Return false if no match found
                checkAllowed = False

    End Select

结束功能

1 个答案:

答案 0 :(得分:4)

当您在For / Next循环中删除集合的成员时,您将遇到同样的问题。

考虑三个对象(幻灯片,主人,苹果,等等)的集合

For Each Object in Collection
  If Object.MeetsSomeCondition Then
    Object.Delete
  End If
Next

因此,第一次通过循环,删除一个对象。 下一次循环,内部计数器正在寻找集合中的第二个对象,但由于集合现在只包含两个对象,它实际上是在看第三个对象是什么。
下一次循环时,内部计数器正在寻找第三个对象,但由于集合中只有两个,因此会出现超出范围的错误。

相反,这样做:

For X = Collection.Count to 1 Step -1
  If condition then Collection(x).delete
Next