• 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 enable event procedures in Outlook?

BobBridges

Active Member
Occasionally I get the ambition to set up something in Outlook that intercepts an event. I've tried it various ways, and so far I haven't gotten Outlook to notice my efforts: When I open an email, or send one, it just opens it or sends it without running or apparently even noticing the routines I wrote.

My latest effort involves trying to intercept the Send action. According to what I read at https://learn.microsoft.com/en-us/office/vba/api/outlook.application.itemsend, I tried creating the following code in a class module:
Code:
Public WithEvents ool As Outlook.Application
Private Sub ool_ItemSend(ByVal oeml As Object, bCan As Boolean)
  MsgBox "ItemSend!"
  End Sub
Sub Class_Initialize()
  Set ool = Outlook.Application
  End Sub
This is in a class module named EnableEvents. Then in a code module I executed a program by the same name:
Code:
Sub EnableEvents()
  Set oee = New EnableEvents
  End Sub
So the EnableEvents class exists, and ool is now an object variable pointing to the Outlook.Application property. When I send an email, I expect the ool_ItemSend method to display a MsgBox; instead it apparently is never run at all.

I know that event procedures are notoriously picky about the syntax, so I suspect I've changed something that's important. But I don't know what. Has anyone here ever got Outlook events working at all? It needn't be for the same purpose; if you got any Outlook event to work, and can show me what I'm doing wrong with ItemSend, I expect I can do the rest.
 
You don't need most of that to simply trap sending emails. In the ThisOutlookSession module, just select Application from the left hand dropdown and ItemSend from the right hand one. That will give you the procedure stub:
Code:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

End Sub

and that should work as soon as you send an item.
 
Ah, that's much better! Thanks heaps, Debaser. I was believing what I read in the documentation, and to tell you the truth I've never paid much attention to those drop-downs, just writing my routines from scratch. Guess I'll have to explore a bit more. Thanks again.
 
You will sneer, but I don't declare every variable I use. I declare if it I want it capitalized in a particular way, and of course if I'm sharing it among multiple procedures or even several "paragraphs" in one long procedure. But for temporary variables I tend to pick short, all-lower-case names that fit my naming conventions and worry no further. So in my programs owb is the workbook, ows the worksheet, ocs is ows.Cells, and in this case oee is just the temporary place-holder for the EnableEvents instance.

Likewise for loop indices I use 'j' and some other letter; so if I'm working through a range I'll probably use jr for the row counter and jc for the column counter.

If I'm working with more than one worksheet (for example), then Dim comes back into play:
Code:
Dim owsFm, owsTo
Set owb = ThisWorkbook
Set owsFm = FetchWorksheet(owb,"Settings")
set owsTo = FetchWorksheet(owb,"New settings")
 
Bit rude to assume I will sneer at you, but that is in fact the cause of your problem. ;) As soon as that EnableEvents code has run, the variable goes out of scope and your object is terminated. That's why it's not handling events.
 
Back
Top