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

Word 2016-使用多个RSCC,如何删除所选内容,但防止在受保护的文档VBA中删除第一部分?

如何解决Word 2016-使用多个RSCC,如何删除所选内容,但防止在受保护的文档VBA中删除第一部分?

我有一个包含多个RepeatingSectionItems的文档。当文档受到保护时,以下代码删除当前选择。但是,我试图找出如何防止第一部分被选中的情况被删除,因为如果删除第一重复部分,我将无法取回它并使所有内容弄乱。

Application.ActiveDocument.Unprotect "green"
'
 Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
  objCC.LockContents = False
  objCC.AllowInsertDeleteSection = True
  '
 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
      'loop through the repeatingsectionitems to find the one that selection is in
      Dim rsi As RepeatingSectionItem
      For Each rsi In CC.RepeatingSectionItems
         If Selection.Range.InRange(rsi.Range) Then
            rsi.Delete
            Exit For
         End If
      Next rsi
   End If
'
Set objCC = ActiveDocument.SelectContentControlsByTitle("General").Item(1)
  objCC.LockContents = True
  objCC.AllowInsertDeleteSection = False
 '
Application.ActiveDocument.Protect wdAllowOnlyFormFields,Password:="green"

解决方法

下面的代码遍历RepeatingSectionItems的集合,如果索引等于1,则显示一个消息框,仅当索引大于1时才删除该项目。

'loop through the repeatingsectionitems to find the one that selection is in
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 are attempting to delete the first item.",Title:="Unable to Delete Item"
        End If
        Exit For
    End If
Next index

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