VBA 宏以突出显示当前电子邮件中的选定文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20624331/
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
VBA macro to highlight selected text in current email message
提问by user3109378
I am trying to create a VBA macro for outlook 2013 that will take the selected text in the e-mail message that I am currently writing (which is in HTML format) and set the font size/color/boldness/highlighting.
我正在尝试为 Outlook 2013 创建一个 VBA 宏,它将采用我当前正在编写的电子邮件消息中的选定文本(采用 HTML 格式)并设置字体大小/颜色/粗体/突出显示。
My macro has two if/then blocks. One block is for Outlook 2003, and gives the desired outcome for all four of the text characteristics. However, after 2003, Outlook uses the Word EditorType for HTML e-mails, and thus I need a different VBA block with different syntax to the change the font of the selected text. The VBA in my 2013 block works correctly for changing the boldness/point size, but it does not apply highlighting to the text. Instead, the command for highlighting the text (rng.Range.HighlightColorIndex = wdYellow) is causing the background color of the selection window to change to clear (so that the text no longer appears to be selected, even though it is still really selected), but no highlighting is applied to the selected text.
我的宏有两个 if/then 块。一个块用于 Outlook 2003,并为所有四个文本特征提供所需的结果。但是,在 2003 年之后,Outlook 使用 Word EditorType 来处理 HTML 电子邮件,因此我需要一个具有不同语法的不同 VBA 块来更改所选文本的字体。我的 2013 块中的 VBA 可以正确更改粗体/磅值,但它不适用于文本突出显示。相反,突出显示文本的命令 (rng.Range.HighlightColorIndex = wdYellow) 导致选择窗口的背景颜色变为清除(这样文本不再看起来被选中,即使它仍然是真正被选中的) ,但不会突出显示所选文本。
When highlighting the text did not work, I tried something else. I tried using the vba command for setting the background to yellow (which has an equivalent visual effect, when manually applied without vba). rng.Shading.BackgroundPatternColor = wdColorYellow. But instead of turning the background yellow, the background changes to black.
当突出显示文本不起作用时,我尝试了其他方法。我尝试使用 vba 命令将背景设置为黄色(在没有 vba 的情况下手动应用时,具有等效的视觉效果)。rng.Shading.BackgroundPatternColor = wdColorYellow。但是背景不是将背景变为黄色,而是变为黑色。
Also the 2013 block does not cause the font color to change. Font color stays black despite the statement (rng.Font.Color = wdColorBlue)
此外,2013 块不会导致字体颜色发生变化。尽管有声明,字体颜色仍为黑色 (rng.Font.Color = wdColorBlue)
Please advise me how I can set the highlight the selected text to yellow and set the color of the selected text to blue.
请告诉我如何将所选文本的突出显示设置为黄色并将所选文本的颜色设置为蓝色。
The full VBA macro is below.
完整的 VBA 宏如下。
Sub ChangeSelectedFontBold14HiYellow()
Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem
If insp.EditorType = olEditorHTML Then ' outlook 2003
Set hed = msg.GetInspector.HTMLEditor
Set rng = hed.Selection.createRange
rng.pasteHTML "<b><font style='color: blue; background: yellow; font-size: 14pt;'>" & rng.Text & "</font></b>"
End If
If insp.EditorType = olEditorWord Then ' outlook 2013
Set hed = msg.GetInspector.WordEditor
Set word = hed.Application
Set rng = word.Selection
rng.Font.Size = 14
rng.Font.Color = wdColorBlue ' color does not change
rng.Font.Bold = True
' rng.Shading.BackgroundPatternColor = wdColorYellow ' changes background color to black instead of yellow
' rng.HighlightColorIndex = wdYellow ' does not work ' error 438 object doesn't support this property
rng.Range.HighlightColorIndex = wdYellow ' does not work - changes the background to clear for the selection indicator color
End If
End If
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
End Sub
回答by Tim Williams
You need to add a VBA Project reference to the Word object library, or define those constants such that Outlook can understand what the true values of wdColorBlue
and wdYellow
are.
你需要一个VBA项目引用添加到Word对象库,或者定义这些常量例如Outlook可以理解的真正价值wdColorBlue
和wdYellow
是。
When I did that, your code had the desired effect (but if you add a reference then you can't use Word
as a variable name)
当我这样做时,您的代码具有预期的效果(但如果您添加引用,则不能Word
用作变量名)
Here's what worked for me (more or less - I was at work when I tested, but not there now...)
The Collapse
part works fine in Word, so shouldwork also in Outlook.
这是对我有用的东西(或多或少 - 我在测试时在工作,但现在不在那里......)该Collapse
部分在 Word 中工作正常,因此也应该在 Outlook 中工作。
Sub ChangeSelectedFontBold14HiYellow()
Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem
If insp.EditorType = olEditorHTML Then ' outlook 2003
Set hed = msg.GetInspector.HTMLEditor
Set rng = hed.Selection.createRange
rng.pasteHTML "<b><font style='color: blue; background: yellow; font-size: 14pt;'>" & rng.Text & "</font></b>"
End If
If insp.EditorType = olEditorWord Then ' outlook 2013
Set hed = msg.GetInspector.WordEditor
Set appWord = hed.Application
Set rng = appWord.Selection
rng.Font.Size = 14
rng.Font.Color = wdColorBlue
rng.Font.Bold = True
rng.Range.HighlightColorIndex = wdYellow
rng.Collapse Direction:=wdCollapseEnd 'UNTESTED, but something like this...
End If
End If
Set appWord = Nothing
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
End Sub