lc.
lc.

Reputation: 116528

event when Outlook mail item is actually closed

I have a .NET application which uses the Microsoft.Office.Interop.Outlook interop assembly.

The application opens a new Outlook MailItem and then would like to record a log entry if the email was sent (via the user clicking Send), or know if it was closed without sending (either saved as a draft or discarded).

Capturing when the item is sent can be done easily enough by subscribing to the ItemEvents_10_Event.Send event.

However, for capturing when the item is closed, I could only find the ItemEvents_10_Event.Close event. But, this event fires when the item is closing, not when the item is closed. Worse yet, the user still has a chance to cancel the action after I receive the event. What happens is:

  1. My application opens the new MailItem.
  2. The user clicks the close button.
  3. ItemEvents_10_Event.Close is fired.
  4. Control returns to Outlook which then presents Outlook's "Do you want to save changes (Yes/No/Cancel)" dialog.
  5. The user presses Cancel. (I get no notification)
  6. The user then can do whatever they want including sending the email or closing it again.

Is there a way to either:

A bonus would be if I could differentiate if the email was closed and discarded vs saved as a draft.

Upvotes: 1

Views: 3536

Answers (1)

Uladzimir Sharyi
Uladzimir Sharyi

Reputation: 154

Use the inspector close event:

inspector = mailItem.GetInspector;
((InspectorEvents_10_Event) inspector).Close += CloseEventHandler;

void CloseEventHandler(){
    //execute only when email window is closed or closing
    // without problem with confirmation dialog (yes/no/cancel)
}

To resolve closing or closed problem, you can check mailItem send event. If it has fired, then "closing", otherwise, user closed window without sending email.

Upvotes: 6

Related Questions