如何在Word中用复选框控件查找/替换符号



我的Word 2010文档中有一个邮件合并字段,该字段包含一个复选框控件,该控件是否被选中取决于输入列表中的内容。

{IF NEW="NEW"☒"☐"}复选框控制

但是,邮件合并完成后,复选框控件将替换为选中或未选中框的符号(视情况而定)。因此,在最终文档中不能再像对复选框控件那样从选中切换到未选中。

这里也问了一个类似的问题,但没有得到解决(一个有解决方案的回答对我来说不起作用)。

我正在寻找一种简单的方法,在输出文档中找到选中或未选中的符号,并将其替换为处于适当状态的复选框文档控件。

我不擅长编程,但如果你能给我指明正确的方向,我会尽力的。我过去玩过VB宏(非常业余),所以我再次尝试,并将其作为"概念验证":

Sub Checkbox()
' ChrW(9744) is unchecked box; 9746 is checked box
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ChrW(9744)
        .Replacement.Text = ChrW(9746)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.HomeKey Unit:=wdStory
End Sub

我还找到了添加复选框控件的方法:

Selection.Range.ContentControls.Add (wdContentControlCheckBox)

但我还没有弄清楚如何将最后一段代码结合到"替换"行中,也没有弄明白如何根据搜索将复选框定义为选中或不选中。

谢谢你的帮助。

您可以执行以下操作:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Do
  With Selection.Find
    .Text = ChrW(9744) ' ChrW(9746)
    .Forward = True
    .Wrap = wdFindStop 'wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  If Selection.Find.Execute = False Then Exit Do
  ' This adds the checkbox at the selected position
  Set f = ActiveDocument.FormFields.Add(Range:=Selection.Range, _
                                        Type:=wdFieldFormCheckBox)      
  'f.CheckBox.Value = True ' Add this for checked checkboxes
Loop

对于Office 2010,提供了一个更高级的复选框控件,支持格式化。其代码为:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Do
  With Selection.Find
    .Text = ChrW(9744)
    .Forward = True
    .Wrap = wdFindStop 'wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  If Selection.Find.Execute = False Then Exit Do
  ' This adds the checkbox at the selected position
  Set f = Selection.Range.ContentControls.Add(wdContentControlCheckBox)
  f.SetCheckedSymbol CharacterNumber:=254, Font _
        :="Wingdings"
  f.SetUncheckedSymbol CharacterNumber:=168, _
        Font:="Wingdings"
  f.Checked = False' Change this for checked checkboxes
Loop

最新更新