【问题标题】:Insert text based on the highlight color in a MS Word file根据 MS Word 文件中的突出显示颜色插入文本
【发布时间】:2023-01-31 01:10:35
【问题描述】:

我正在尝试分析 Word 文件文本块已被用户突出显示在一组颜色中,以检查突出显示文本的颜色是否有图案。

我使用 selection.Find 循环来匹配感兴趣的颜色,然后插入文本标记当亮点开始和结束时。

文件在同一个句子中不均匀地突出显示文本块,有时在颜色变化之间没有字符

Sub clickforcolors()
'Finds highlighted text and inserts text before and after displaying the color
'Find some highlighted text:
    With selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .MatchWildcards = False
        .Forward = True
        .Wrap = wdFindContinue
        .Highlight = True
        Do
            .Execute
'Check that it's the colors we want
        Loop Until selection.Range.HighlightColorIndex = wdYellow Or selection.Range.HighlightColorIndex = wdRed Or selection.Range.HighlightColorIndex = wdBrightGreen _
          Or Not .Found
'Select that chunk and insert the text needed according to the color
        selection.Range.Select
        If selection.Range.HighlightColorIndex = wdYellow Then
            selection.InsertBefore (" /// BegginingY")
            selection.InsertAfter (" EndY ///")
        ElseIf selection.Range.HighlightColorIndex = wdRed Then
            selection.InsertBefore (" /// BegginingR")
            selection.InsertAfter (" EndR ///")
        ElseIf selection.Range.HighlightColorIndex = wdBrightGreen Then
            selection.InsertBefore (" /// BegginingG")
            selection.InsertAfter (" EndG ///")
        End If
    End With
'make sure that the cursor is at the end of the selected text, so that running this macro again will find another text:
    selection.Collapse Direction:=wdCollapseEnd

End Sub

这适用于一大块彩色文本。如果它有效,我可以在每个文件中使用宏 30 次。

有两个问题

  1. 代码只在红色的块。
  2. 有两个时不起作用同一句话中的不同颜色.
    我知道这个问题与范围有关,但我还没有找到任何其他选项来管理它。

【问题讨论】:

    标签: vba ms-word highlight


    【解决方案1】:

    我最近遇到了类似的问题。使用 Sentences 而不是 Range 并使用带有 .Text="*" 的通配符来修复它。

    希望能帮助到你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 2010-10-18
      • 1970-01-01
      • 2019-07-04
      • 1970-01-01
      • 2012-06-28
      • 2016-03-29
      • 2018-10-03
      相关资源
      最近更新 更多