如何使用 VBA 的 HighlightColorIndex 开始和停止突出显示文本

发布于 2025-01-01 02:02:13 字数 1042 浏览 0 评论 0原文

我正在从另一个应用程序中提取文本并即时创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2025-01-08 02:02:13

您需要像之前一样选择文本并将其突出显示重置为无 wdNoHighlight

使用以下代码

' >>> This is where I need to cease highlighting but what to do?
'{funky code to stop highlighting here}
Selection.Move WdUnits.wdCharacter, 1
''Clear for text
lngRangeStart = Selection.Start
Selection.TypeText text:="Now back to clear text"
lngRangeEnd = Selection.Start
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdNoHighlight

Selection.Move WdUnits.wdCharacter, 1
Selection.TypeText text:="Now back to the future text"

You need to select text as you did before and reset its highlight to none wdNoHighlight

Use below code

' >>> This is where I need to cease highlighting but what to do?
'{funky code to stop highlighting here}
Selection.Move WdUnits.wdCharacter, 1
''Clear for text
lngRangeStart = Selection.Start
Selection.TypeText text:="Now back to clear text"
lngRangeEnd = Selection.Start
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdNoHighlight

Selection.Move WdUnits.wdCharacter, 1
Selection.TypeText text:="Now back to the future text"
不忘初心 2025-01-08 02:02:13

如果我正确理解你的问题,那么你只需将突出显示颜色设置为 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 to wdColorAutomatic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文