0
votes

I'm following the samples shown on MSDN but I'm missing a part because I can't get an "event" to be triggered.

The goal is to append the timestamp and username at the end of the message each time a user opens/preview/mark as read a mail item.

I've read MailItem.Open (https://msdn.microsoft.com/en-us/library/office/ff865989.aspx) and MailItem.Read (https://msdn.microsoft.com/EN-US/library/office/ff869872.aspx). I've disabled macro security (Enable All macros).

My code in "ThisOutlookSession". I'm restarting Outlook each time I change something:

Public WithEvents SelectedItem As Outlook.MailItem

Sub Initialize_handler()
 Set SelectedItem = Application.ActiveExplorer.CurrentFolder.Items(1)
 SelectedItem.Display
End Sub

Sub SelectedItem_Read()
  MsgBox "Trigger by MailItem.Read"
End Sub

Sub SelectedItem_Open()
  MsgBox "Trigger by MailItem.Open"
End Sub

I can't get the msgbox to display in any cases of open/preview for 3 seconds/mark as read by the user.

When the events work, I plan to call the following sub:

Sub MessageWasOpenned()
    Dim myItem As MailItem
    Set SelectedItems = ActiveExplorer.Selection
    For Each myItem In SelectedItems
    myItem.Body = myItem.Body & vbCrLf & "This message was opened by: " & (Environ$("Username")) & " on: " & Now
    myItem.Save
    Next
End Sub
1
I am also a newbie to VBA . You may like to see Listing 17.2. Stamp the date and current user on an Outlook item <msdn.microsoft.com/en-us/library/dd492012(v=office.12).aspx>skkakkar

1 Answers

0
votes

You set up the event handler on the very first item in the folder, whatever it happens to be. Most likely it will be the oldest message in the folder, but the order is not determined unless you explicitly sort the Items collection. That order has nothing to do with what you see in Outlook's Explorer.

If you use the Selection collection, Open event will fire.