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

冷暖自知 提交于 2021-01-29 14:28:56

问题


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.)


回答1:


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



回答2:


Not sure if this does what you want but see if there's anything useful in there...

Sub Demo()

    Const DEF_STYLE As String = "myCCStyle"
    
    Dim cc As ContentControl, s As Style, sP As Style
    
    'Adjust the built-in style used by the placeholder text
    With ActiveDocument.Styles("Placeholder text").Font
        .Color = wdColorGreen
        .Size = 11
        .Name = "Times New Roman"
    End With
    
    
    On Error Resume Next
    Set s = ActiveDocument.Styles(DEF_STYLE) 'see if our style exists
    On Error GoTo 0
    'add if not already there
    If s Is Nothing Then Set s = ActiveDocument.Styles.Add(DEF_STYLE, Type:=wdStyleTypeCharacter)
    With s.Font
        'just to be different....
        .Name = "Courier"
        .Size = 15
        .ColorIndex = wdBlue
    End With
    
    'set the default style (for non-placeholder content)
    For Each cc In ActiveDocument.ContentControls
        If cc.Type = wdContentControlDropdownList Or cc.Type = wdContentControlComboBox Then
            cc.DefaultTextStyle = DEF_STYLE
        End If
    Next cc

End Sub


来源:https://stackoverflow.com/questions/63162374/how-do-i-change-a-microsoft-word-content-control-placeholder-text-format-using-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!