Friday 14 October 2016

Outlook VBA: Work with Open Item or Selected Item

To work with the currently selected item (not opened) in the message list, you will need to reference the ActiveExplorer and will use the ActiveExplorer method to get the item in the Explorer window. (This is used for all Outlook items that are selected but not opened: appointments, contacts, tasks, etc.)
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
Selected items uses the Explorer
When you are working with an Outlook item that is open and has focus, it uses the ActiveInspector method. The code you need to work with Inspectors is.
Set objItem = objApp.ActiveInspector.CurrentItem

inspector
To help remember which is which, think of the main Outlook window as Windows Explorer. When you work with items in this window, you'll use Explorer.

When you want to use the macro with either open or selected items, you can use this function to determine if the item is open or selected. You'll call the function in your code in this manner:

Set objItem = GetCurrentItem()
You'll need to put one copy of this function in a module and can use the function in many macros.
VBA
15 lines
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
End Select
Set objApp = Nothing
End Function
These articles are applicable to all versions of Outlook:

0 comments:

Post a Comment