微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Word- VBA- 如果仅剩一个节,如何防止删除选定的重复节内容控件?

如何解决Word- VBA- 如果仅剩一个节,如何防止删除选定的重复节内容控件?

以下代码成功删除了选定的 RSCC,但始终阻止删除一个 RSCC。

 Dim cc As ContentControl
    If Selection.information(wdInContentControl) Then
        Set cc = Selection.ParentContentControl
        If Not cc.Type = wdContentControlRepeatingSection Then
            Do Until cc.Type = wdContentControlRepeatingSection
                Set cc = cc.ParentContentControl
            Loop
        End If
        Dim index As Long
        For index = 1 To cc.RepeatingSectionItems.Count
            If Selection.Range.InRange(cc.RepeatingSectionItems(index).Range) Then
                If index > 1 Then
                    cc.RepeatingSectionItems(index).Delete
                Else
                    MsgBox Prompt:="You cannot delete this.",Title:="Error"
                End If
                Exit For
            End If
        Next index
    End If

我的目标是能够删除任何选定的 RSCC,但不能删除任何剩余的 RSCC。

换句话说,如果我有三个 RSCC (1,2,3),而不是总是保护第 1 节,如果我要删除第 1 节和第 3 节,我想保护第 2 节或保护第 3 节如果第1 和 2 被删除

解决方法

   Dim cc As ContentControl
   If Selection.Information(wdInContentControl) Then
      Set cc = Selection.ParentContentControl
      If Not cc.Type = wdContentControlRepeatingSection Then
         Do Until cc.Type = wdContentControlRepeatingSection
            Set cc = cc.ParentContentControl
         Loop
      End If
      If cc.RepeatingSectionItems.count > 1 Then
         Dim index As Long
         Dim count As Long
         count = cc.RepeatingSectionItems.count
         For index = cc.RepeatingSectionItems.count To 1 Step -1
            If Selection.Range.InRange(cc.RepeatingSectionItems(index).Range) Then
               If count > 1 Then
                  cc.RepeatingSectionItems(index).Delete
                  count = count - 1
               Else
                  MsgBox Prompt:="There is only 1 item left so you cannot delete it.",Title:="Error"
               End If
            End If
         Next index
      Else
         MsgBox Prompt:="There is only 1 item left so you cannot delete it.",Title:="Error"
      End If
   End If

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。