How to capture a Mailitem Event

The Outlook developer reference for MailItem.Close event includes the 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 a message - but it did not run. I have no knowledge of using Outlook events (other than events in the Application class which run 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.

"rattanjits" <rattanjits.3wiysa@no-spam.invalid> wrote in message

news:rattanjits.3wiysa@no-spam.invalid...

> The Outlook developer reference for MailItem.Close event includes the
> 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 a
> message - but it did not run. I have no knowledge of using Outlook
> events (other than events in the Application class which run
> automatically). Please advise.

> > rattanjits
> https://forums.slipstick.com/
>
 
Thank you Ken. I am able to capture inspector and mailitem events now.



However I cannot get the mailitem.propertychange event to work. Keep getting a compile error "Procedure declaration does not match description of event or procedure having the same name" Have tried various declarations such as:


Private Sub myItem_PropertyChange(Name As String) 'this is the syntax in the Object browser.


Private Sub myItem_PropertyChange(Saved) 'Saved is the Property name


Private Sub myItem_PropertyChange(myProperty) 'Using a string declared and valued earlier.




Other other events are working with myItem. Does PropertyChange need 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.

"rattanjits" <rattanjits.3wks2t@no-spam.invalid> wrote in message

news:rattanjits.3wks2t@no-spam.invalid...

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

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

> Other other events are working with myItem. Does PropertyChange need
> anything extra?

> > rattanjits
> https://forums.slipstick.com/
>
 

Similar threads

Top