如何使用 VBA 的 HighlightColorIndex 开始和停止突出显示文本
我正在从另一个应用程序中提取文本并即时创建 MS-Word 文档。
有时,当我发现这些单词时,可能会突出显示所需的单词。我无法理解的是如何停止显示HighlightColorIndex。
我尝试过 Selection.Collapse、Selection.Range.Collapse 和 Selection.Range.HighlightColorIndex = wdNoHighlight 都取得了有限的成功。你能帮忙吗?
Dim lngRangeStart As Long
Dim lngRangeEnd As Long
Selection.TypeText Text:="Test of colour" ' No highlighting at present
Selection.TypeParagraph '
Selection.TypeText Text:="Starting colour after colon: " ' No highlighting at present
lngRangeStart = Selection.Start ' set to the start of the Range
Selection.Range.StartOf
Selection.TypeText Text:="This text is highlighted"
lngRangeEnd = Selection.Start ' set to the end of the Range and sel.start appears correct
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdYellow
' >>> This is where I need to cease highlighting but what to do?
{funky code to stop highlighting here}
Selection.TypeText Text:="Now back to clear text"
I am pulling text from another application and creating a MS-Word document on the fly.
Occasionally there may be some highlighting of words needed which I perform as I find these. What I cannot understand is how to cease displaying the HighlightColorIndex.
I've tried Selection.Collapse, Selection.Range.Collapse and Selection.Range.HighlightColorIndex = wdNoHighlight all to limited success. Can you assist please?
Dim lngRangeStart As Long
Dim lngRangeEnd As Long
Selection.TypeText Text:="Test of colour" ' No highlighting at present
Selection.TypeParagraph '
Selection.TypeText Text:="Starting colour after colon: " ' No highlighting at present
lngRangeStart = Selection.Start ' set to the start of the Range
Selection.Range.StartOf
Selection.TypeText Text:="This text is highlighted"
lngRangeEnd = Selection.Start ' set to the end of the Range and sel.start appears correct
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdYellow
' >>> This is where I need to cease highlighting but what to do?
{funky code to stop highlighting here}
Selection.TypeText Text:="Now back to clear text"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要像之前一样选择文本并将其突出显示重置为无
wdNoHighlight
使用以下代码
You need to select text as you did before and reset its highlight to none
wdNoHighlight
Use below code
如果我正确理解你的问题,那么你只需将突出显示颜色设置为 wdColorAutomatic ,这是一个指定自动(默认)颜色的常量。
因此,将它们放在一起,为了突出显示文本,您可以将其背景设置为
wdColorYellow
。要删除突出显示,您可以将其背景设置为wdColorAutomatic
。If I understand your question correctly, then you just set the highlight color to
wdColorAutomatic
, which is a constant specifying the automatic (default) color.So putting it all together, to highlight text, you'd set its background to
wdColorYellow
. To remove the highlighting, you'd set its background towdColorAutomatic
.