Consulting

Results 1 to 8 of 8

Thread: Changing font of all content control placeholders with VBA

  1. #1

    Changing font of all content control placeholders with VBA

    Hi

    As per the title, my form users sometimes don't fill in every field. So I wanted to make it abundently clear of the content controls that are not yet filled in. So I thought maybe I could use the ContentControlOnExit to look at all content controls with text that remains the same a placeholder and make it a different colour.

    I have tried this:

     For Each CC In ActiveDocument.Range.ContentControls        If CC.Range.Text = CC.PlaceholderText Then
               CC.Range.Font.ColorIndex = wdBrightGreen
               
            End If
        Exit For
        Next CC
    However this does not seem to do what I expected (it does nothing)

    Can anyone advise where I am going wrong?

    Many thanks!

  2. #2
    VBAX Regular
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    39
    Location
    Try changing the Placeholder Text style.

  3. #3
    Hi Chas

    Thanks for the response. I assume that this is just done in "design" mode and just changing the font of the placeholder?

    If this is the case, can you (or anyone) still advise what I would do to achieve this, or similar using VBA just so I understand where I am going wrong?

    Many thanks

  4. #4
    Word has a built-in style called 'Placeholder Text'. You can display all the styles and right click the Placeholder Text stylename in the list and select Modify to format it as you wish for that template. Subsequently if the control is showing the placeholder, it will be styled with that style.
    If you want to set that style with VBA then
     ActiveDocument.Styles("Placeholder Text").Font.ColorIndex = wdBrightGreen
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Hi Graham

    Thanks for taking the time to respond.

    Just so I understand, in some of my other codes I am stating if CC.placeholdertext = "X" Then do something which does work. However, using a similar priniciple the For Each does not work. Where am I going wrong with this?

    Thanks again

  6. #6
    I am no longer sure what you are trying to do. If you want to change the format of the placeholder text as suggested by your code example, then you only need to change the placeholder text style - once! - as in my last message.
    If you want to do something if any of the content controls are displaying the placeholder text then
    Dim CC As ContentControl
        For Each CC In ActiveDocument.Range.ContentControls
            If CC.ShowingPlaceholderText = True Then
                MsgBox "True" 'do something
            End If
        Next CC
    If as in your code sample you include Exit For outside the condition it will only process the first content control and ignore the rest. If you use Exit For inside the condition it will only process the first control that matches the condition.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Thanks Graham

    I get it! Thank you!

    In the meantime I did change manaully the placeholder color by right clicking on it in design mode. This does change the text as desired but it also changes the user's inputted text to this color as well. I guess I will use the VBA you suggested (just once) to change all the placeholder text.

    Many thanks once again.

  8. #8
    VBAX Regular
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    39
    Location
    Placeholder text is designed to be replaced by something else. By design it is gray but you can set it any color, again by modifying the style. This is not done in design mode. Graham gave you the code to do that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •