vba无法清除单词ContentControl内容

vba cannot clear the word ContentControl content

contentcontrol“内容无法编辑”复选框没有勾选,我用代码设置

LockContets = False

,但即便如此,仍然出现错误“您不能编辑此选择,因为它受到保护”

代码如下:

Sub Test()
  Dim CC As ContentControl  
  For Each CC In ActiveDocument.ContentControls
      Debug.Print CC.Type
      Debug.Print CC.Range.Text
      CC.LockContentControl = True
      CC.LockContents = False
      CC.Range.Text = ""    <--error here


  Next CC
End Sub

为什么会这样?如何解决?

有很多类型的内容控件,您只需要添加一个 if 条件来检查内容控件类型是否为文本

Sub Test()
  Dim CC As ContentControl
  For Each CC In ActiveDocument.ContentControls
      CC.LockContents = False
      If CC.Type = wdContentControlRichText Or CC.Type = wdContentControlText Then
        CC.Range.Text = ""
      End If
  Next CC
End Sub

您无法清除下拉列表、复选框或图片内容控件中的文本,因为它们没有可编辑的文本属性。

尝试以下方法:

Sub Test()
Dim CC As ContentControl
For Each CC In ActiveDocument.ContentControls
  With CC
    .LockContentControl = True
    .LockContents = False
    Select Case .Type
      Case wdContentControlRichText, wdContentControlText, wdContentControlComboBox, wdContentControlDate
        .Range.Text = ""
      Case wdContentControlDropdownList
        .Type = wdContentControlText
        .Range.Text = ""
        .Type = wdContentControlDropdownList
      Case wdContentControlCheckBox: .Checked = False
      Case wdContentControlPicture: .Range.InlineShapes(1).Delete
    End Select
  End With
Next CC
End Sub