• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to implement an Outlook event?

BobBridges

Active Member
I'm experimenting with VBA/Outlook, and would like to try setting up an event handler for mail items. I can make events work in Excel, and I'm reading the Outlook documentation, but I can't seem to get any Outlook mailitem events fire.

Part of what puzzles me is that it says the event module is to be named xxx_Send (for example), where xxx is the mailitem to be detected. But that assumes that I'm going to name the mail item xxx, every time I use this particular event. Can that be right?

And even if it is, I don't seem to be able to make it work. I think I followed the instructions, but none of the events I've tried to do fire when I execute a Sub to trigger them. Has anyone else fooled with events for Outlook?
 
Yeah, I guess I could have done that in the first place and saved time. Sorry about that. In the ThisOutlookSession module is this (having removed comments):
Code:
Public WithEvents myItem As Outlook.MailItem

Private Sub myItem_Open(bCan As Boolean)
  MsgBox "Email open!"
  End Sub

Private Sub myItem_Read()
  MsgBox "Email read!"
  End Sub
In another module I try to trigger one of the events:
Code:
Sub Main()
  Set myItem = EmailStart("me@nowhere.com", "", "Hi!")
  End Sub

Function EmailStart(AddrTo, AddrCc, Subj)
  Set oeml = Application.CreateItem(0) 'olMailItem
  Set oeml.SendUsingAccount = Application.Session.Accounts.Item("<my account>")
  oeml.To = AddrTo
  oeml.CC = AddrCc
  oeml.Subject = Subj
  oeml.GetInspector.Activate
  Set EmailStart = oeml
  End Function
Eventually I want to use the Send event, but I'm working up to it gradually.

That EmailStart function is one I use frequently in Excel, so I know it works generally. The part I've never done before is Outlook events. Stepping through this creates the email alright, but doesn't seem to trigger the events.
 
You should be using:

Code:
Sub Main()
  Set ThisOutlookSession.myItem = EmailStart("me@nowhere.com", "", "Hi!")
End Sub

but that code won't trigger either of those events since the email is already open before you assign the myItem variable.
 
How is that? The email cannot be open ~before~ I assign the myItem variable: Assigning the myItem variable ~is~ opening it.

Oh, wait a second—maybe I see what you mean. The subroutine creates the email, and opens it in the process, and only after that in the main routine is the assignment made to myItem. Right?

Hmm. I'll noodle on that and (probably) come back for more. Thanks.
 
Well, my main Sub looks like this now and it didn't trigger either event routine:
Code:
  Set myItem = Application.CreateItem(0) 'olMailItem
  Set myItem.SendUsingAccount = Application.Session.Accounts.Item("robhbridges@gmail.com (1)")
  myItem.To = "me@nowhere.com"
  myItem.CC = AddrCc
  myItem.Subject = "Hi!"
  myItem.GetInspector.Activate
Next idea?

Oh, wait, your other thing; I should create myItem as ThisOutlookSession.myItem? Strange. Sure, why not?...
 
ThisOutlookSession is a class, so to access members of it, you have to refer to them as members of the class. If you were using Option Explicit, the compiler would have warned you about your undeclared variable(s).
 
I'm back, after a few days' hiatus. I modified my main routine to look like this:
Code:
Sub Main()
  Set ThisOutlookSession.myItem = Application.CreateItem(0) 'olMailItem
  ThisOutlookSession.myItem.GetInspector.Activate
  Set ThisOutlookSession.myItem.SendUsingAccount = Application.Session.Accounts.Item("robhbridges@gmail.com (1)")
  ThisOutlookSession.myItem.To = "me@nowhere.com"
  ThisOutlookSession.myItem.CC = AddrCc
  ThisOutlookSession.myItem.Subject = "Hi!"
  End Sub
..and that triggered the Open event when I activated the inspector. So yeah, on the right track.

I'm still a little puzzled. I looked in the Outlook documentation for an object named ThisOutlookSession (or Session, or Outlook), and didn't find anything. Then I examined it in the VBE, and saw that ThisOutlookSession is simply Application—the Outlook Application, presumably. I see that one of the properties of the Outlook application is Session, but when I look that up in the documentation it says the Session property is actually a Namespace object.

Oh, well. I see that if I start thus:
Code:
Set ThisOutlookSession.myItem = Application.CreateItem(0) 'olMailItem
Set oeml = ThisOutlookSession.myItem
...then it recognizes the desired connection; I can use oeml in the rest of the routine, and the Send method will trigger the Send event. Thanks, Debaser. I may come back with more questions eventually, but for now I think I'm off and running.

(If you care, the next task is to figure out how to communicate between Outlook and Excel about the Send status. Excel is in this case the client; can I get Outlook to act as a server? Thinking about ways to get inter-app communication going. I'm not asking you, I'm asking myself—but if you want to comment, feel free.)
 
I'm still a little puzzled. I looked in the Outlook documentation for an object named ThisOutlookSession (or Session, or Outlook), and didn't find anything.


It's a built-in object:
83820

(If you care, the next task is to figure out how to communicate between Outlook and Excel about the Send status. Excel is in this case the client; can I get Outlook to act as a server? Thinking about ways to get inter-app communication going. I'm not asking you, I'm asking myself—but if you want to comment, feel free.)

If you want to do the monitoring and responding to the event in Excel, then your WithEvents variable needs to be declared and set in a class in Excel, not in Outlook.
 
Back
Top