0

我有一组自定义布局,它们是我认可的企业标准。有 1 个主布局和 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

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

子清理模板()

    '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

结束子

函数 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

结束功能

4

1 回答 1

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
于 2012-09-25T14:46:57.250 回答