2

I am working on a VSTO Outlook2010 client addin. I have a ribbon and it shows up in new email explorer window. I need to write a method that does something when the new email window is closed. I know the window closes on its own when the email is sent, but I need a method that fires up when window is closed without the email being send, such as when the user wants to cancel out.

Any idea with sample code would be highly appreciated.

I tried this from another posts. below 2 lines says: cannot implicitly convert type . An explicit conversion exists. Are you missing a cast?

Inspector OpenedMailItem;
//within the start-up method 
Inspector insp = this.Application.Inspectors; 
insp.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(OnNew‌​Inspector);


//new function, within class
public void OnNewInspector(Outlook.Inspector inspector) 
{ 
   if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem) 
   {  
       OpenedMailItem = inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;               
       ((ItemEvents_10_Event)OpenedMailItem).Close += new Outlook.ItemEvents_10_CloseEventHandler(MailItem_Close); 
   } 
 } 

void(MailItem)
{ 
    //something 
} 

Got so many errors and hovering over sasy: cannot implicitly convert type . An explicit conversion exists. Are you missing a cast?

I am importing all this:

using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Linq; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Excel = Microsoft.Office.Interop.Excel; 
using System.Windows.Forms; using System.Net; 
using System.Web; using System.IO; 
using System.Text.RegularExpressions; 
using Redemption; 
using System.Runtime.InteropServices; 
using Microsoft.Office.Interop.Outlook; 
using Exception = System.Exception;
Polzi
  • 77
  • 8

1 Answers1

0

Trap the Application.Inspectors.NewInspector event, add the new inspector to a list of inspectors. Listen for the Inspector.Close event. When it fires, remove the inspector from the list.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • You might want to update your original post to make sure the code is properly formatted. What is the exact error? – Dmitry Streblechenko Aug 22 '14 at 16:19
  • I have updated the main post. I get: cannot implicitly convert type . An explicit conversion exists. Are you missing a cast? on assigning variable : insp and OpenMailItem. Thanks – Polzi Aug 22 '14 at 20:37
  • 1
    It tells you to explicitly cast to MailItem: OpenedMailItem = (MailItem)inspector.CurrentItem as ... Keep in mind that it will only work if you are really editing a mail item. It will fail if you edit a contact, appointment, etc. – Dmitry Streblechenko Aug 23 '14 at 22:10
  • can you add an example-code how to do "When it fires, remove the inspector from the list."? Because in the close-event you do not have any possibility to access the closing-"inspector" - or do you? – Gerwald Oct 18 '14 at 18:18
  • The close event fires on a particular instance of the Inspector object. Instead of adding the actual Inspector object to the list, create your wrapper class that has Inspector as a member variable and has the Inspector.Close event handler. When the event fires, you can remove the wrapper object (this) from the list. – Dmitry Streblechenko Oct 19 '14 at 18:35