找到以术语开头的行并将其删除

时间:2013-12-17 12:25:16

标签: vba ms-word word-vba

我正在尝试自动截断两个人之间的长谈话,这些谈话已被粘贴到单词中。

输入

Jim , 18th November 05:23

Hi!

Sarah , 19th November 06:03

Hello there

我想格式化Jim blue中的所有文本,以及来自Sarah red的所有文本。为了做到这一点,我想我需要遵循这个过程:

  1. 将每个文字的颜色设为红色
  2. 找到以“Jim”开头的行,并将所有内容格式化为蓝色,直到它到达以“Sarah”开头的行。

1 个答案:

答案 0 :(得分:2)

希望这有帮助

Public Sub ColorMeRedBlue()
    Selection.HomeKey Unit:=wdStory

    color_the_text = False

    For Each line_in_para In ActiveDocument.Paragraphs

        text_in_line = line_in_para.Range.Text
        check_name = Split(text_in_line, " ")

        If InStr("Jim", check_name(0)) Then
            color_the_text = False
        ElseIf InStr("Sarah", check_name(0)) Then
            color_the_text = True
        End If

        If color_the_text = False Then
            Selection.HomeKey Unit:=wdLine
            Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.Font.Color = wdColorRed
            Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.EndKey Unit:=wdLine
            Selection.MoveDown Unit:=wdLine, Count:=1
        ElseIf color_the_text = True Then
            Selection.HomeKey Unit:=wdLine
            Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.Font.Color = wdColorBlue
            Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            Selection.EndKey Unit:=wdLine
            Selection.MoveDown Unit:=wdLine, Count:=1
        End If

    Next line_in_para

End Sub