2

我正在尝试为 Outlook 2013 创建一个 VBA 宏,它将采用我当前正在编写的电子邮件消息中的选定文本(采用 HTML 格式)并设置字体大小/颜色/粗体/突出显示。

我的宏有两个 if/then 块。一个块用于 Outlook 2003,并为所有四个文本特征提供所需的结果。但是,在 2003 年之后,Outlook 对 HTML 电子邮件使用 Word EditorType,因此我需要一个具有不同语法的不同 VBA 块来更改所选文本的字体。我的 2013 块中的 VBA 可以正常工作以更改粗体/磅值,但它不适用于文本突出显示。相反,用于突出显示文本的命令 (rng.Range.HighlightColorIndex = wdYellow) 导致选择窗口的背景颜色变为清除(因此文本似乎不再被选中,即使它仍然被真正选中) ,但不会对所选文本应用突出显示。

当突出显示文本不起作用时,我尝试了其他方法。我尝试使用 vba 命令将背景设置为黄色(在没有 vba 的情况下手动应用时,它具有等效的视觉效果)。rng.Shading.BackgroundPatternColor = wdColorYellow。但不是将背景变为黄色,而是将背景变为黑色。

此外,2013 块不会导致字体颜色发生变化。尽管有声明,字体颜色仍保持黑色 (rng.Font.Color = wdColorBlue)

请告诉我如何将所选文本的突出显示设置为黄色并将所选文本的颜色设置为蓝色。

完整的 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
4

1 回答 1

5

您需要添加对 Word 对象库的 VBA 项目引用,或定义这些常量,以便 Outlook 可以理解和的真正值是wdColorBlue什么wdYellow

当我这样做时,您的代码具有预期的效果(但如果您添加引用,则不能Word用作变量名)

这对我有用(或多或少 - 我在测试时正在工作,但现在不在那里......)该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
于 2013-12-17T04:22:16.283 回答