How to capture a Mailitem Event

R

rattanjits

The Outlook developer reference for MailItem.Close event includes th
following example to save an item without prompting the user:

Public WithEvents myItem As Outlook.MailItem
Public Sub Initalize_Handler()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myItem_Close(Cancel As Boolean)
If Not myItem.Saved Then myItem.Save
End Sub

I copied the example into Thisoutlooksession - and editted and closed
message - but it did not run. I have no knowledge of using Outloo
events (other than events in the Application class which ru
automatically). Please advise
 
K

Ken Slovak - [MVP - Outlook]

You would need to put the cursor in the Initialize_Handler() procedure and
press F5 to run the code to initialize the event handler manually to test
the code. An item would need to be open at that time.

To make this automatic and initialized on Outlook startup you would need to
add additional code. This code in ThisOutlookSession would run on Outlook
startup and initialize the needed event handlers for that:

Public WithEvents colInspectors As Outlook.Inspectors
Public WithEvents myInspector As Outlook.Inspector

Private Sub Application_Startup()
Set colInspectors = Application.Inspectors
End Sub

Private Sub col_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then ' only for mail items
Set myInspector = Inspector
End If
End Sub

Private Sub myInspector_Close()
If myInspector.CurrentItem.Saved = False Then
myInspector.CurrentItem.Save
End If

Set myInspector = Nothing
End Sub

This would check for Saved on each mail item when it's closed. It would only
handle 1 open mail item at a time, and it would only handle mail items.
 
R

rattanjits

Thank you Ken. I am able to capture inspector and mailitem events now.

However I cannot get the mailitem.propertychange event to work. Kee
getting a compile error "Procedure declaration does not matc
description of event or procedure having the same name" Have trie
various declarations such as:
Private Sub myItem_PropertyChange(Name As String) 'this is the synta
in the Object browser.
Private Sub myItem_PropertyChange(Saved) 'Saved is the Property name
Private Sub myItem_PropertyChange(myProperty) 'Using a string declare
and valued earlier.

Other other events are working with myItem. Does PropertyChange nee
anything extra
 
K

Ken Slovak - [MVP - Outlook]

Private Sub oMail_PropertyChange(ByVal Name As String)

All you have to do is select oMail or whatever other object that's declared
WithEvents in the General drop-down at the top left of the code window and
all of the object's events are exposed in the Declarations (right)
drop-down. Select the event and a template is inserted into your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top