如何开始和停止使用VBA的HighlightColorIndex突出显示文本
我正在从另一个应用程序中提取文本,并动态创建MS Word文档 偶尔会有一些我发现需要的单词的突出显示。我无法理解的是如何停止显示HighlightColorIndex 我尝试了Selection.Collapse、Selection.Range.Collapse和Selection.Range.HighlightColorIndex=wdNoHighlight all以获得有限的成功。你能帮忙吗如何开始和停止使用VBA的HighlightColorIndex突出显示文本,vba,ms-word,Vba,Ms Word,我正在从另一个应用程序中提取文本,并动态创建MS Word文档 偶尔会有一些我发现需要的单词的突出显示。我无法理解的是如何停止显示HighlightColorIndex 我尝试了Selection.Collapse、Selection.Range.Collapse和Selection.Range.HighlightColorIndex=wdNoHighlight all以获得有限的成功。你能帮忙吗 Dim lngRangeStart As Long Dim lngRangeEnd As Long
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"
如果我正确理解了您的问题,那么您只需将高光颜色设置为WDCOLORSAUTOMATIC,这是一个指定自动默认颜色的常量
所以把它们放在一起,为了突出显示文本,您需要将其背景设置为wdColorYellow。要删除突出显示,您需要将其背景设置为wdColorAutomatic。您需要像以前一样选择文本,并将其突出显示重置为无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"