Word 2016- 如何 add/delete 在 VBA 中控制重复节内容

Word 2016- How to add/delete Repeating Section Content Control in VBA

正在尝试编写 vba 代码来添加和删除特定命名的 RSCC 的重复项目部分。

以下代码适用于添加但无法弄清楚如何删除

此宏将插入重复节 CC:

Sub AddRepeatingSectionCC()
  Dim oCC As ContentControl
  Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRepeatingSection, Selection.Range)
  With oCC
    .AllowInsertDeleteSection = True
    .RepeatingSectionItemTitle = "Repeating Section Item"
  End With
  Set oCC = Nothing
End Sub

这是设置其他 CC 选项的第二个宏:

Sub SetOptions()
  If Selection.Information(wdInContentControl) Then
    With Selection.ParentContentControl
      'Sets the appearance to the original bounding box look. For the newer tags look, use wdContentControlTags
      'If you don't need to change a setting, comment it out before running the macro
      .Appearance = wdContentControlBoundingBox

      'Sets the color of the control to a preset color
      .Color = wdColorWhite

      'Sets whether the Content Control can be deleted or not. If the control has had .Temporary = True applied, you must reverse that property to True before applying this.
      .LockContentControl = True

      'Sets whether the contents of the Content Control can be deleted or not.
      .LockContents = False

      'Sets the placeholder text or prompt for the control
      .SetPlaceholderText , , "Default Text"

      'Sets the Content Control tag property
      .Tag = "Tag"

      'If this is set to true, the Content Control will be removed when the contents are edited.
      .Temporary = False

      'Sets the title of the Content Control. This appears on a tab above the control when it is activated.
      .Title = "Title"
    End With
  Else
    MsgBox "Please select a Content Control to change its options."
  End If
End Sub

要删除它:

Sub DeleteCC()
  If Selection.Information(wdInContentControl) Then
    Selection.ParentContentControl.Delete
  Else
    MsgBox "Please select a Content Control to delete it."
  End If
End Sub

要删除特别命名的 RSCC 的重复项目部分,只需编辑您在其他问题中找到的代码。

Set cc = ActiveDocument.SelectContentControlsByTitle("RepCC").Item(1)
'to delete the first RepeatingSectionItem
cc.RepeatingSectionItems.Item(1).Delete
'to delete all but the first RepeatingSectionItem
Dim index As Long
For index = cc.RepeatingSectionItems.Count To 2 Step -1
  cc.RepeatingSectionItems.Item(index).Delete
Next index
'to delete the entire ContentControl
cc.Delete