0

我有一个包含多个 的文档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"
4

1 回答 1

0

下面的代码循环遍历集合,RepeatingSectionItems如果索引等于一则显示一个消息框,并且仅在索引大于一时删除该项。

'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
于 2020-11-04T20:02:05.030 回答