Help!

How to capture a Mailitem Event

 
  

Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Programming VBA RSS
Next:  Hide details for a shared calendar  
Author Message
rattanjits
External


Since: Aug 06, 2009
Posts: 1



PostPosted: Thu Aug 06, 2009 9:32 pm    Post subject: How to capture a Mailitem Event
Archived from groups: microsoft>public>outlook>program_vba (more info?)

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
http://forums.slipstick.com/
Back to top
Ken Slovak - [MVP - Outlo
External


Since: Oct 17, 2003
Posts: 3355



PostPosted: Fri Aug 07, 2009 10:00 am    Post subject: Re: How to capture a Mailitem Event [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"rattanjits" <rattanjits.3wiysa.RemoveThis@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
> http://forums.slipstick.com/
>
Back to top
rattanjits
External


Since: Aug 07, 2009
Posts: 1



PostPosted: Fri Aug 07, 2009 9:00 pm    Post subject: Re: How to capture a Mailitem Event [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
http://forums.slipstick.com/
Back to top
Ken Slovak - [MVP - Outlo
External


Since: Oct 17, 2003
Posts: 3355



PostPosted: Sat Aug 08, 2009 4:24 pm    Post subject: Re: How to capture a Mailitem Event [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"rattanjits" <rattanjits.3wks2t RemoveThis @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
> http://forums.slipstick.com/
>
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> Programming VBA All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum