How do you get a reference to the mail item in the current open window in Outlook using VBA?

两盒软妹~` 提交于 2019-11-28 07:36:21

问题


I have a macro that works very well to place into folders / apply flags / set categories, but it only works on the current item selected in the explorer.

When I get an email alert on my desktop and click on it to open the email message, I would like to be able to run the same macro against that open item, but I can't find any documentation on how to access that object in a similar way to how I access the selected item in the explorer list.

My current selection logic looks like this:

Dim Item As Object
Dim SelectedItems As Selection

Set SelectedItems = Outlook.ActiveExplorer.Selection
For Each Item In SelectedItems
    With Item
        'do stuff
    End With
Next Item

回答1:


Apparently this is the code to get the current open item:

If TypeName(Application.ActiveWindow) = "Inspector" Then
    Set Item = Application.ActiveWindow.CurrentItem



回答2:


I did it like this. Declare the Item as a MailItem instead of an Object and then you get help from IntelliSense.

Dim CurrentMessage As MailItem
Set CurrentMessage = ActiveInspector.CurrentItem
CurrentMessage.HTMLBody = "[Insert HTML here]" 


来源:https://stackoverflow.com/questions/4134967/how-do-you-get-a-reference-to-the-mail-item-in-the-current-open-window-in-outloo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!