Search code examples
vbams-wordword-contentcontrol

How do I change a Microsoft Word Content Control placeholder text format using a VBA script?


I have created a Microsoft Word Form using interactive Content Controls. It is a really long form with a variety of fields and field types (drop down lists, text boxes, combo boxes, check boxes, etc...) After a couple of months and multiple edits it came back to me with inconsistent formatting of field values and placeholder text values. Some fields stay grayed out even after you fill them out. Other fields have placeholder text that is not grayed out at all. The font seems to have inconsistent size, style and shade of gray. I can edit placeholder text font style and size in Design Mode, but I cannot figure out how to make sure that placeholder text is grayed out (same shade of gray) and fields value text is not. In addition, I am thinking of automating the process using a VBA macro. I use the following macro shared by Greg Maxey to set placeholder text a lot:

Sub SetPlaceHolderText()
Dim strText As String
  If Selection.Range.ContentControls.Count = 1 Then
    On Error GoTo Err_Handler
    With Selection.Range.ContentControls(1)
      strText = .PlaceholderText.Value
      .SetPlaceHolderText , , InputBox("Type your new placeholder text below.", _
                              "Define Placeholder Text", strText)
    End With
  Else
    MsgBox "You must select a single ContentControl." & vbCr + vbCr _
           & "Click the ""empty"" or ""title"" tag of the" _
           & " ContentControl you want to modify."
  End If
Exit Sub
Err_Handler:
End Sub

It can be found here: https://gregmaxey.com/word_tip_pages/modify_cc_placeholder_text.html.

Can something similar be done to apply the default Word Content Control formatting (such as placeholder text takes paragraph default formatting and a certain shade of gray until it is overwritten at which time it is no longer grayed out)? I have close to 80 Content Controls in that form and hope to streamline the process without having to start from scratch. I would appreciate any suggestion, either using a VBA script or Word object properties. Thank you.

Edit:

I created a simple macro that loops through my fields and changes fonts for both, placeholders and values but it does not change all the defaults. As I mentioned in my comments, I when I select something then go back to the placeholder by selecting the first dropdown item that is by default the same as the placeholder the format gets reset again. Here is the script:

Sub Demo()

Dim cc As ContentControl

For Each cc In ActiveDocument.ContentControls
    If cc.Type = wdContentControlDropdownList Or cc.Type = wdContentControlComboBox Then
        If cc.ShowingPlaceholderText Then
            With cc.Range.Font
                .Name = "Times New Roman"
                .Size = 11
                .ColorIndex = wdGray50
            End With
        Else
            With cc.Range.Font
                .Name = "Times New Roman"
                .Size = 11
                .ColorIndex = wdBlack
            End With
        End If
    End If
Next cc

I am not at all familiar with Microsoft Word but I was able to create the form and extract data. However, so many changes were made to it by other people (which in turn messed up some of the build in formatting) that rolling back to my "clean" version and starting over would be a lot of work. I would like to "fix" the existing form if possible. What other Word Content Control font defaults am I missing?

Edit:

It would also be nice to loop through Date Picker fields and apply standard formatting and placeholder text as well. For some reason when I include:

If cc.Type = wdContentControlDropdownList Or cc.Type = wdContentControlComboBox Or cc.Type = wdContentControlDate Then

my date placeholder text gets formatted the same way as Content Control value text (in my example above it is wdBlack.)


Solution

  • To reset the placeholder text loop through the content controls and write the text currently in the control to a variable. Delete the text in control and set the placeholder text to the value of the variable. This will reset the flag and the Placeholder Text style will automatically be applied. This will also ensure that the control behavior gets reset so that clicking into it replaces the placeholder text.

    Sub ResetCCPlaceholderText()
      Dim cc As ContentControl
      Dim promptText As String
      For Each cc In ActiveDocument.ContentControls
        if cc.Type = wdContentControlDate then
            promptText = cc.Range.Text
            cc.Range.Text = ""
            cc.SetPlaceholderText Text:=promptText
        end if
      Next cc
    End Sub