none
How to change the default properties of content controls?

    Question

  • I'm tired of changing content control properties.  Does anybody know how to change them so they are already have the "Use a Style to format" and "Content control cannot be deleted" checked, and the "Click here to enter text" removed?
    Tuesday, May 15, 2012 8:12 PM

Answers

  • In order of appearance in your post:

    The subroutine should be Public if you want to assign a shortcut or a button to run it, or use the macros (F8) dialog. Since subroutines are Public by default, you can omit that keyword and just start with "Sub". You would use Private if the subroutine was meant to be called by other code in the same module, but not from outside the module.

    The comment about setting rng to the range you want can generally be ignored. The behavior of the built-in control-insertion buttons is to insert at the Selection, so the macro should do the same. However, in general the ContentControls.Add method isn't restricted to putting the control at the Selection; it might be useful in some situations to insert at a bookmark, or in a particular table cell, etc., without having to move the Selection to that location.

    The LockContentControl property does correspond to the "control cannot be deleted" check box in the Properties dialog. The LockContents property is the one that corresponds to "contents cannot be edited". I'm not sure why you couldn't type in the control, but it may help to set both properties explicitly.

    You can't assign a value directly to the PlaceholderText property; instead you use the SetPlaceholderText method and supply one of the three possible arguments.

    Sub InsertMyContentControl()
          Dim rng As Range
          Dim objCC As ContentControl

          Set rng = Selection.Range
         
          Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
     
          With objCC
             .Tag = "mytag"
             .LockContentControl = True
             .LockContents = False
             .DefaultTextStyle = ActiveDocument.Styles("Heading 2")
             .SetPlaceholderText Text:="Type"
          End With
    End Sub


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org

    • Marked as answer by Sandi V Thursday, May 17, 2012 1:29 PM
    Wednesday, May 16, 2012 11:55 PM
    1. Do you want that every time you click on the Ribbon | Develop | controls (for inserting a Content Control at the Selection) the Content Control inserted has already the properties you want?
    2. Do you want to set those properties programmatically via VBA?

    I think the best way to achieve 1) is repurposing the controls in the Ribbon you are interested in. You may give a look at Temporarily Repurpose Commands on the Office Fluent Ribbon

    If you want to achieve 2) you can use  a procedure of this type:

    Private Sub InsertMyContentControl()
       Dim rng As Range
       Dim objCC As ContentControl
    
       Set rng = Selection.Range
       'set the range you want
       
       Set objCC = ActiveDocument.ContentControls.ADD(wdContentControlRichText, rng)
       ' chose the Content Contro you want insert between:
       'wdContentControlBuildingBlockGallery
       'wdContentControlCheckbox
       'wdContentControlComboBox
       'wdContentControlDate
       'wdContentControlGroup
       'wdContentControlDropdownList
       'wdContentControlPicture
       'wdContentControlRichText
       'wdContentControlText
    
       With objCC
          .tag = "mytag"
          .LockContentControl = True
        '  .etc
        ' look at the other properties you could set
       End With
    
    
    End Sub
    

    By Lauro

    • Marked as answer by Sandi V Wednesday, May 16, 2012 8:33 PM
    Tuesday, May 15, 2012 8:41 PM
  • Hi Sandi

    There's no way to change the defaults in the Options dialog box, so Lauro's suggestion isn't a bad one :-) And since you've posted this in an MSDN Word for Developers forum it's also understandable that anyone trying to help would offer you a code solution. For other types of solutions/approaches it would be better to post in the forum that supports end-users on Answers:

    http://answers.microsoft.com/en-us/office/forum/word

    That said, if you don't want to use VBA you could, perhaps create Building Block (AutoText) entries for each type of control, with your preferred settings?


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Sandi V Wednesday, May 16, 2012 8:33 PM
    Wednesday, May 16, 2012 5:19 AM

All replies

    1. Do you want that every time you click on the Ribbon | Develop | controls (for inserting a Content Control at the Selection) the Content Control inserted has already the properties you want?
    2. Do you want to set those properties programmatically via VBA?

    I think the best way to achieve 1) is repurposing the controls in the Ribbon you are interested in. You may give a look at Temporarily Repurpose Commands on the Office Fluent Ribbon

    If you want to achieve 2) you can use  a procedure of this type:

    Private Sub InsertMyContentControl()
       Dim rng As Range
       Dim objCC As ContentControl
    
       Set rng = Selection.Range
       'set the range you want
       
       Set objCC = ActiveDocument.ContentControls.ADD(wdContentControlRichText, rng)
       ' chose the Content Contro you want insert between:
       'wdContentControlBuildingBlockGallery
       'wdContentControlCheckbox
       'wdContentControlComboBox
       'wdContentControlDate
       'wdContentControlGroup
       'wdContentControlDropdownList
       'wdContentControlPicture
       'wdContentControlRichText
       'wdContentControlText
    
       With objCC
          .tag = "mytag"
          .LockContentControl = True
        '  .etc
        ' look at the other properties you could set
       End With
    
    
    End Sub
    

    By Lauro

    • Marked as answer by Sandi V Wednesday, May 16, 2012 8:33 PM
    Tuesday, May 15, 2012 8:41 PM
  • Thanks for the quick reply!  I prefer to avoid VBA whever I can, and would like the settings to be permanent.  I'll look closer at your suggestions in the morning.  Thanks!
    Tuesday, May 15, 2012 8:53 PM
  • Hi Sandi

    There's no way to change the defaults in the Options dialog box, so Lauro's suggestion isn't a bad one :-) And since you've posted this in an MSDN Word for Developers forum it's also understandable that anyone trying to help would offer you a code solution. For other types of solutions/approaches it would be better to post in the forum that supports end-users on Answers:

    http://answers.microsoft.com/en-us/office/forum/word

    That said, if you don't want to use VBA you could, perhaps create Building Block (AutoText) entries for each type of control, with your preferred settings?


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by Sandi V Wednesday, May 16, 2012 8:33 PM
    Wednesday, May 16, 2012 5:19 AM
  • Thanks, Cindy.  I assumed the answer would be in VBA, regardless of my preferences ;)  Hence, my post here.  Your building blocks suggestion addressed my immediate need.  Thanks very much; don't know why I didn't think of it!  But I'd like to get Lauro's solution working.  I've been working much of the day trying to help myself with this, hoping I didn't have to come begging for more, but here I am!  I really appreciate all you guys do!  Here's the trouble I'm having:

       Private Sub InsertMyContentControl() 'I changed this from public to private_
       'is that a problem? (I couldn't see the macro when it was private).
          Dim rng As Range
          Dim objCC As ContentControl

          Set rng = Selection.Range
          'set the range you want NO ERROR, but I'm not sure what to do with this_
         
          Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
     
          With objCC
             .Tag = mytag
             .LockContentControl = True 'I originally assumed this was what was checking the "control cannot be deleted"_
             'box, but I've discovered that user cannot type in the control._
             'Even when I comment it out, user can't type in the control.
             .DefaultTextStyle = ActiveDocument.Styles("Heading 2")
             .PlaceholderText = "Type" 'ERROR 5861, placeholder text is a read only property
          End With

       End Sub

    THANKS FOR YOUR HELP!

    Wednesday, May 16, 2012 8:58 PM
  • In order of appearance in your post:

    The subroutine should be Public if you want to assign a shortcut or a button to run it, or use the macros (F8) dialog. Since subroutines are Public by default, you can omit that keyword and just start with "Sub". You would use Private if the subroutine was meant to be called by other code in the same module, but not from outside the module.

    The comment about setting rng to the range you want can generally be ignored. The behavior of the built-in control-insertion buttons is to insert at the Selection, so the macro should do the same. However, in general the ContentControls.Add method isn't restricted to putting the control at the Selection; it might be useful in some situations to insert at a bookmark, or in a particular table cell, etc., without having to move the Selection to that location.

    The LockContentControl property does correspond to the "control cannot be deleted" check box in the Properties dialog. The LockContents property is the one that corresponds to "contents cannot be edited". I'm not sure why you couldn't type in the control, but it may help to set both properties explicitly.

    You can't assign a value directly to the PlaceholderText property; instead you use the SetPlaceholderText method and supply one of the three possible arguments.

    Sub InsertMyContentControl()
          Dim rng As Range
          Dim objCC As ContentControl

          Set rng = Selection.Range
         
          Set objCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, rng)
     
          With objCC
             .Tag = "mytag"
             .LockContentControl = True
             .LockContents = False
             .DefaultTextStyle = ActiveDocument.Styles("Heading 2")
             .SetPlaceholderText Text:="Type"
          End With
    End Sub


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org

    • Marked as answer by Sandi V Thursday, May 17, 2012 1:29 PM
    Wednesday, May 16, 2012 11:55 PM
  • Aha!  All points crystal clear, Jay, and I've got it working just as expected.  Thank you very much! 
    Thursday, May 17, 2012 1:29 PM