vba 如何将要在 Word 2007 中键入的文本的突出显示颜色切换为黄色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12670190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to switch the highlight color to yellow for text that will be typed in Word 2007?
提问by MostlyHarmless
In a VBA-Macro in MS Word 2007 I want switch the text highlight color to yellow for the text that will be typed next at the cursor position.
在 MS Word 2007 中的 VBA-Macro 中,我想将文本突出显示颜色切换为黄色,以便接下来在光标位置键入的文本。
If I want to change the color of the currently selected text, I can do that with
Selection.Range.HighlightColorIndex = wdYellow
如果我想更改当前选定文本的颜色,我可以这样做
Selection.Range.HighlightColorIndex = wdYellow
But that only seems to work if the text already exists and is selected. Can I also set the highlight color to yellow for the text that will be inserted at the cursor position?
但这似乎只有在文本已经存在并被选中时才有效。我还可以将要插入光标位置的文本的突出显示颜色设置为黄色吗?
(E. g. if I click on the "bold font" button, all future text will be bold, but that does not seem to work with the highlight function, or is there a way to do it?)
(例如,如果我点击“粗体”按钮,所有未来的文本都将是粗体,但这似乎不适用于突出显示功能,或者有办法做到这一点吗?)
回答by GSerg
all future text will be bold
所有未来的文字都将是粗体
No, it won't. By clicking bold
you mark the current point in the text as bold, and if you carry on typing from that point, the text will be bold because it is being typed from a bold place, not because the button is pressed. If you click bold
, move the caret to some other place and type, it won't be bold.
不,不会。通过单击bold
将文本中的当前点标记为粗体,如果您从该点继续键入,文本将变为粗体,因为它是从粗体位置键入的,而不是因为按下了按钮。如果单击bold
,将插入符号移动到其他位置并键入,它不会是粗体。
The correct way is to insert the text and apply formatting to its range.
正确的方法是插入文本并将格式应用于其范围。
Dim r As Range
Set r = Selection.Range
r.Collapse wdCollapseStart
r.InsertAfter "Text to insert"
r.HighlightColorIndex = wdYellow