6

I have created a macro, that does certain things, to an opened mail. I would like to create a rule, that does it automatically, when I open the mail.

I don't want this rule to run all time, just when I open a mail, I don't want to force this rule on each mail I receive.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ZZA
  • 129
  • 1
  • 11

1 Answers1

6

Following @ZZA comments,

try this code:

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)

   'Your code
End Sub

Paste code in ThisOutlookSession enter image description here

EDIT

To avoid 'Your code triggering the event we need an event disabler:

Public WithEvents myItem As Outlook.MailItem
Public EventsDisable as Boolean

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


Private Sub myItem_Open(Cancel As Boolean)
    EventsDisable=True
   'Your code
    EventsDisable=False
End Sub
hstay
  • 1,439
  • 1
  • 11
  • 20
  • Hi, I have tried to test it by just adding: msgbox "test" to it, but it doesn't work, and I can't run this macro, cause it takes you to the screen, where you have to select which macro to run. – ZZA Feb 12 '14 at 12:51
  • Did you paste your code in `ThisOutlookSession` object? – hstay Feb 12 '14 at 13:14
  • Yeah, I did, but I had to restart outlook, and now it works, but it doesn't do what I need it to be done. When I select a mail, the messagebox pops up, and not when I open the mail. – ZZA Feb 12 '14 at 13:30
  • Okay, now I have added: Set msg = Application.ActiveExplorer.Selection(1), where msg is an outlook.mailitem I think a way to do it, is to determine if the msg is opened, displayed, but find no way. – ZZA Feb 12 '14 at 14:16
  • 1
    You got it! I was struggling with Set msg = Application.ActiveExplorer.Selection(1). The key is the Open event: http://msdn.microsoft.com/en-us/library/office/ff865989.aspx – hstay Feb 12 '14 at 14:35
  • Did you test it? It still doesn't work for me. tested with the usual msgbox, but nothing happens. And after I've read the msdn article, I am confused a bit, does it only work, if the mail is unread? – ZZA Feb 12 '14 at 14:54
  • It is working for me at the moment. The only drawback is that new emails are triggered as well. – hstay Feb 12 '14 at 15:05
  • Now i see, you have edited again your answer. It works fine with msgbox, but If i kust paste a very short code, it doesn't work. Maybe I used bad declarations? . . Dim reply As Outlook.MailItem Dim Original As Outlook.MailItem Set Original = Application.ActiveExplorer.Selection(1) Set reply = myItem.reply reply.Display reply.OriginatorDeliveryReportRequested = True – ZZA Feb 12 '14 at 15:14
  • 1
    See my last edit. When you create a reply you are triggering the event Application_ItemLoad. And your code is a pretty handy script! Can I keep it? :) – hstay Feb 12 '14 at 15:26
  • 1
    Also be careful with `Set Original= Application.ActiveExplorer.Selection(1)`, it should read `Set Original=myItem` – hstay Feb 12 '14 at 15:31
  • Thank you for helping me :) It works fine now, but I have to go, and cant test furthermore. If I have anymore problems I may comment again tomorrow. Use it :) – ZZA Feb 12 '14 at 15:44
  • Hi, somehow the myItem_Open() seems to be not triggered when I use this code. Has something changed in Outlook 2016? – bo-oz Jul 04 '17 at 09:21
  • Works great, thank you @hstay. I used the code in the edit and it works perfect – Mark Feb 23 '21 at 02:47