Word VBA:强调子弹/段落中的特定单词

时间:2018-01-09 07:37:41

标签: vba ms-word word-vba

所以我们假设我们有一个只有1列的表。在每个单元格中,可以有许多句子的子弹(指针)如下:

  • 特别是教师欺凌(10月):201720/11/20
  • 特别是员工疯狂(11月):2016/22/1021
  • 关于学生逃学的反复反馈(12月):201711/12/12

我已经能够在子弹(段落对象)之间进行迭代并得到每个句子;

但我想知道的是......有没有办法强调之间的'特别是'或'反复'反馈':'已发生?

代码如下:

Dim objParagraph As Paragraph,rangeStart As Integer,rangeEnd As Integer

For Each.Range.Paragraphs中的每个objParagraph   如果objParagraph.Range.Text喜欢“Recurring *”那么

rangeStart = Len("Recurring feedback on ") +1

否则

rangeStart = Len("In particular to ") +1

结束如果

rangeEnd = Len(左(objParagraph.Range.Text,              InStr函数(objParagraph.Range.Text, “:” - 1))

'格式范围(开始,结束).underlined = true?

下一个objParagraph

抱歉英文不好:/

1 个答案:

答案 0 :(得分:0)

好,

我已经修改了一下,以下内容可以帮助你。

Dim ran As Range, rstart As Long, rend As Long

Set ran = ActiveDocument.Content

With ran
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .text = "In particular to "
        .Replacement.text = ""
        .MatchWildcards = False
        .MatchCase = False
        .Wrap = wdFindStop
        .Execute
    End With

    While .Find.Found
        rstart = .End
        .Collapse wdCollapseEnd
        .Find.text = ":"
        .Find.Execute
        rend = .Start
        ActiveDocument.Range(rstart, rend).Font.Underline = wdUnderlineSingle
        .Collapse wdCollapseEnd
        .Find.text = "In particular to "
        .Find.Execute
    Wend

    .Find.text = "Recurring feedback on "
    .Find.Execute
    While .Find.Found
        rstart = .End
        .Collapse wdCollapseEnd
        .Find.text = ":"
        .Find.Execute
        rend = .Start
        ActiveDocument.Range(rstart, rend).Font.Underline = wdUnderlineSingle
        .Collapse wdCollapseEnd
        .Find.text = "Recurring feedback on "
        .Find.Execute
    Wend

End With

如果您希望将处理限制为表格,只需替换

Set ran = ActiveDocument.Content

Set ran = ActiveDocument.Tables(1).Range