【发布时间】: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 次。
有两个问题
- 代码只在红色的块。
- 有两个时不起作用同一句话中的不同颜色.
我知道这个问题与范围有关,但我还没有找到任何其他选项来管理它。
【问题讨论】: