How to capture a Mailitem Event

Status
Not open for further replies.
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/
>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
JorgeDario How to capture and save the text, when responding a MailItem? Outlook VBA and Custom Forms 3
R Capture Sender's Display name and Address Outlook VBA and Custom Forms 3
M Using field names to capture a data element Using Outlook 0
I Capture incoming emails Outlook VBA and Custom Forms 1
A Capture network traffic in IE to file Outlook VBA and Custom Forms 5
G Capture "forward event" ? Outlook VBA and Custom Forms 11
J VBA Form in Outlook to capture data and complile a mail Outlook VBA and Custom Forms 2
A how to capture events for next item Outlook VBA and Custom Forms 10
B Capture Inbound E-mail Subject with VBA Outlook VBA and Custom Forms 4
A how to capture outlook 2007 events Outlook VBA and Custom Forms 4
T Some way to capture who marks an email as read Outlook VBA and Custom Forms 1
C Event Capture - Item Context Menu (Delete). Can you hook it? Outlook VBA and Custom Forms 1
G Event when creating task from mailitem Outlook VBA and Custom Forms 2
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
U Outbox Message Stuck after reading some MailItem Properties with VBA Outlook VBA and Custom Forms 1
oliv- Best practice for catching mailitem.events Outlook VBA and Custom Forms 0
oliv- How to select an mailitem in explorer with "show as conversation" Outlook VBA and Custom Forms 8
JorgeDario how to check a MailItem has a digital signature (SMIME) with vba? Outlook VBA and Custom Forms 1
JorgeDario ¿What property of mailitem can be used like primary key? Outlook VBA and Custom Forms 6
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
B right click outlook objects in OL2010 acts on current inbox mailitem Using Outlook 6
C MailItem Find method doesn't work Using Outlook 0
C MailItem.SaveAs not working Outlook VBA and Custom Forms 10
G RE:The signature is also inserted if you touch the MailItem. Outlook VBA and Custom Forms 1
B Add signature to MailItem Outlook VBA and Custom Forms 3
C How can I create a new MailItem inside a user folder? Outlook VBA and Custom Forms 4
S Create a new Outlook MailItem in an Outlook folder(not a draft) Outlook VBA and Custom Forms 2
A How to get OOM MailItem Raw data Outlook VBA and Custom Forms 2
S Saved Property of MailItem is copied Outlook VBA and Custom Forms 1
S MailItem Find Method question Outlook VBA and Custom Forms 6
N Getting the attachments in MailItem Outlook VBA and Custom Forms 1
T How to get MailItem.Body without security warning in Outlook 2010 Outlook VBA and Custom Forms 2
S ->[O2007] Parsing each line of a MailItem HTMLBody? Outlook VBA and Custom Forms 2
T How to get Inspector or MailItem from wordEditor Outlook VBA and Custom Forms 6
A Select the position of an attached file in a HTML mailitem Outlook VBA and Custom Forms 1
M MailItem object has no property for when a reply was sent Outlook VBA and Custom Forms 3
B Insert information to MailItem Outlook VBA and Custom Forms 1
E Properties added to MailItem in ItemSend event visible to recipien Outlook VBA and Custom Forms 1
V Setting HTMLBody of new mailItem Outlook VBA and Custom Forms 1
V How to find mailitem in the inspector is a brand new one Outlook VBA and Custom Forms 2
M Activate "Add digital signature to this massage" on a MailItem? Outlook VBA and Custom Forms 1
K importing EML in MailItem Outlook VBA and Custom Forms 1
A mailitem Send issue Outlook VBA and Custom Forms 5
M Get email address from MailItem.To? Outlook VBA and Custom Forms 6
S UserProperties of MailItem object. Outlook VBA and Custom Forms 3
S get current position in message body of mailitem Outlook VBA and Custom Forms 8
S How to get RFC822 format message from the MailItem object. Outlook VBA and Custom Forms 4
J Toolbar button to process current mailitem Outlook VBA and Custom Forms 1
D MailItem from an RSS feed Outlook VBA and Custom Forms 2
J Outlook 2007 crashed when pushing send on a displayed mailitem Outlook VBA and Custom Forms 1

Similar threads

Top