首页 > 解决方案 > Word 2016-如何在 VBA 中添加/删除重复部分内容控件

问题描述

尝试编写 vba 代码来添加和删除专门命名的 RSCC 的重复项部分。

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

是否可以使用 VBA 添加重复部分内容控制部分?

标签: vbams-word

解决方案


这个宏将插入一个重复的部分 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


推荐阅读