在Outlook 2013(VBA)中突出显示阅读窗格中的文本

时间:2016-07-13 21:00:14

标签: vba email outlook

我是VBA的新手,并将一堆代码拼凑在一起以实现我的目标。我试图在Outlook的阅读器窗格中突出显示特定文本,因此当您单击特定电子邮件时,阅读器将显示电子邮件的正文以及突出显示的任何文本,在本例中为“Test”一词。

目前,没有任何反应。但如果我双击电子邮件在新窗口中打开它,现在可以正确突出显示阅读面板中的预览。看起来好像它在打开时触发的事件不是预览,然后更改将在不再位于顶部的窗口上生效。

以下是我目前的代码:

Public WithEvents myItem As Outlook.MailItem

Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olMail Then
    Set myItem = Item
End If
End Sub


Private Sub myItem_Open(Cancel As Boolean)
EventsDisable = True

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 = olEditorWord Then
Set hed = msg.GetInspector.WordEditor
Set appWord = hed.Application
Set rng = appWord.Selection
rng.Find.HitHighlight ("Test")
End If

End If

Set appWord = Nothing
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
EventsDisable = False

End Sub

1 个答案:

答案 0 :(得分:0)

不使用ActiveInspector - 它与Explorer显示的项目无关。 请尝试以下

set hed = Application.ActiveExplorer.Selection(1).GetInspector.WordEditor

请注意,如果项目在检查器中显示并且在活动资源管理器中选择了,则上述更改将仅在检查器中突出显示文本,预览窗格不会受到影响。如果您希望始终使用预览窗格,则可能需要查看Redemption及其SafeExplorer对象,该对象明确显示阅读窗格:

set sExplorer = CreateObject("Redemption.SafeExplorer")
sExplorer.Item = Application.ActiveExplorer
set hed = sExplorer.ReadingPane.WordEditor
Set appWord = hed.Application
Set rng = appWord.Selection
rng.Find.HitHighlight ("Test")