简体   繁体   中英

vba cannot clear the word ContentControl content

The contentcontrol "contents cannot be edited" checkbox is unchecked, and I use code to set

LockContets = False

, but even so, there is still error "You are not allowed to edit this selection because it is protected"

the code is as follow:

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

why will this happen? how to solve it?

There are many type of conent controls all you need is add an if condition to check if content coontrol type is of text

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

You cannot clear the text from Dropdown List, CheckBox, or Picture Content Controls, since they don't have an editable text property.

Try something along the lines of:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM