如何使用 VBA 脚本更改 Microsoft Word 内容控件占位符文本格式? [英] How do I change a Microsoft Word Content Control placeholder text format using a VBA script?

查看:146
本文介绍了如何使用 VBA 脚本更改 Microsoft Word 内容控件占位符文本格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用交互式内容控件创建了一个 Microsoft Word 表单.这是一个非常长的表单,包含各种字段和字段类型(下拉列表、文本框、组合框、复选框等......)几个月后,经过多次编辑,它以不一致的格式返回给我字段值和占位符文本值.即使您填写了某些字段,它们仍然是灰色的.其他字段具有完全不灰显的占位符文本.字体的大小、样式和灰色阴影似乎不一致.我可以在设计模式下编辑占位符文本字体样式和大小,但我无法弄清楚如何确保占位符文本变灰(相同的灰色阴影)并且字段值文本不是.此外,我正在考虑使用 VBA 宏自动化该过程.我使用以下 Greg Maxey 共享的宏来设置占位符文本:

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

可以在这里找到:https://gregmaxey.com/word_tip_pages/modify_cc_placeholder_text.html.

是否可以执行类似的操作来应用默认的 Word 内容控件格式(例如占位符文本采用段落默认格式和某种灰色阴影,直到它被覆盖,此时它不再变灰)?我有近 80 个这种形式的内容控件,希望无需从头开始即可简化流程.我很感激任何建议,无论是使用 VBA 脚本还是 Word 对象属性.谢谢.

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.

我创建了一个简单的宏,它循环遍历我的字段并更改占位符和值的字体,但它不会更改所有默认值.正如我在评论中提到的,当我选择某些内容时,我会通过选择第一个下拉项返回到占位符,默认情况下该下拉项与格式再次重置的占位符相同.这是脚本:

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

我对 Microsoft Word 一点也不熟悉,但我能够创建表单并提取数据.但是,其他人对其进行了如此多的更改(这反过来弄乱了一些格式的构建),这些更改回滚到我的干净"版本.版本并重新开始将是很多工作.我想修复"如果可能,现有表格.我还缺少哪些其他 Word 内容控件字体默认值?

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?

循环遍历日期选择器字段并应用标准格式和占位符文本也很好.出于某种原因,当我包括:

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

我的日期占位符文本的格式与内容控件值文本的格式相同(在我上面的示例中,它是 wdBlack.)

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

推荐答案

通过内容控件重置占位符文本循环并将控件中的当前文本写入变量.删除控件中的文本并将占位符文本设置为变量的值.这将重置标志并自动应用占位符文本样式.这也将确保控件行为被重置,以便点击它替换占位符文本.

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

这篇关于如何使用 VBA 脚本更改 Microsoft Word 内容控件占位符文本格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆