PDA

View Full Version : Changing font of all content control placeholders with VBA



Trader174
07-22-2021, 12:24 PM
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!

Chas Kenyon
07-22-2021, 03:39 PM
Try changing the Placeholder Text style.

Trader174
07-22-2021, 11:40 PM
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

gmayor
07-23-2021, 01:20 AM
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

Trader174
07-23-2021, 02:24 AM
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

gmayor
07-23-2021, 02:41 AM
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 CCIf 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.

Trader174
07-23-2021, 03:16 AM
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.

Chas Kenyon
07-26-2021, 02:18 PM
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.