none
NewInspector cancel creation of the new email RRS feed

  • Question

  • this if from my addin on startup:

     private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    
    
     Globals.ThisAddIn.Application.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    
    }
    
    here the new Inspector:
    public void Inspectors_NewInspector(Outlook.Inspector inspector)
    {
     var item = inspector.CurrentItem as Outlook.MailItem;
     if (item != null)
     {
      // ***
     }
    }
    

     

    *** We have a wpf application behind our software, that is because we wan't to keep the addin's "dump".. and in the wpf application I create a new MailItem. Everything works perefct. The only thing I have to do is to stop the event at this point because at the moment I get 2 new Mails ;) one created with our software and one new because of the new event from outlook.

    how can I stop the event at this point? I searched for hours and tried everything I found.

    Thank you for your help.

     

    Regards




    Tuesday, July 19, 2011 1:26 PM

Answers

  • Hello,

    The Outlook object model allows us to get the item in NewInspector, connect to the item's Open event and then cancel Open. Ken, please correct me if I'm wrong.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by patricci_ Tuesday, July 19, 2011 4:22 PM
    Tuesday, July 19, 2011 2:38 PM
  • Hi Andrei,
     
    That's quite correct. However closing the item will leave an Inspector open with no item open in it. You still need to close the Inspector window. It's been a while but you might be able to call Inspector.Close() in the item.Open() handler, I haven't tried that in many years. Even so, you'd still get at least some flicker when the Inspector opened briefly and then closed.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Andrei Smolin" <=?utf-8?B?QW5kcmVpIFNtb2xpbg==?=> wrote in message news:afbbbc88-4fc3-486e-ba46-a587aeacab3c...

    Hello,

    The Outlook object model allows us to get the item in NewInspector, connect to the item's Open event and then cancel Open. Ken, please correct me if I'm wrong.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader

    Ken Slovak MVP - Outlook
    • Marked as answer by patricci_ Tuesday, July 19, 2011 4:22 PM
    Tuesday, July 19, 2011 2:53 PM

All replies

  • Using the Outlook Object Model you can't stop the Inspector, there's no Cancel argument passed to that event handler. You could set up to handle the Inspector.Activate() event and call Inspector.Close() there.
     
    At the point NewInspector() fires you usually don't have a window opened yet that you can find using FindWindow(), if you could you could use the Win32 API call PostMesage() to send a WM_CLOSE message to the window. But in most cases the window hWnd can't be found until the first Inspector.Activate() fires.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "patricci_" <=?utf-8?B?cGF0cmljY2lf?=> wrote in message news:9501ef44-7583-46fa-8756-da72d9800135...

    this if from my addin on startup:

     private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    
    
     Globals.ThisAddIn.Application.Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    
    }
    
    here the new Inspector:
    public void Inspectors_NewInspector(Outlook.Inspector inspector)
    {
     var item = inspector.CurrentItem as Outlook.MailItem;
     if (item != null)
     {
     // ***
     }
    }
    

     

    *** We have a wpf application behind our software, that is because we wan't to keep the addin's "dump".. and in the wpf application I create a new MailItem. Everything works perefct. The only thing I have to do is to stop the event at this point because at the moment I get 2 new Mails ;) one created with our software and one new because of the new event from outlook.

    how can I stop the event at this point? I searched for hours and tried everything I found.

    Thank you for your help.

     

    Regards





    Ken Slovak MVP - Outlook
    Tuesday, July 19, 2011 1:35 PM
  • Hello,

    The Outlook object model allows us to get the item in NewInspector, connect to the item's Open event and then cancel Open. Ken, please correct me if I'm wrong.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    • Marked as answer by patricci_ Tuesday, July 19, 2011 4:22 PM
    Tuesday, July 19, 2011 2:38 PM
  • Hi Andrei,
     
    That's quite correct. However closing the item will leave an Inspector open with no item open in it. You still need to close the Inspector window. It's been a while but you might be able to call Inspector.Close() in the item.Open() handler, I haven't tried that in many years. Even so, you'd still get at least some flicker when the Inspector opened briefly and then closed.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Andrei Smolin" <=?utf-8?B?QW5kcmVpIFNtb2xpbg==?=> wrote in message news:afbbbc88-4fc3-486e-ba46-a587aeacab3c...

    Hello,

    The Outlook object model allows us to get the item in NewInspector, connect to the item's Open event and then cancel Open. Ken, please correct me if I'm wrong.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader

    Ken Slovak MVP - Outlook
    • Marked as answer by patricci_ Tuesday, July 19, 2011 4:22 PM
    Tuesday, July 19, 2011 2:53 PM
  • See if you get the correct Inspector if you use item.GetInspector() in the item.Open() event handler Andrei suggested using. Then you can call Inspector.Close() if necessary from that Inspector reference.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "patricci_" <=?utf-8?B?cGF0cmljY2lf?=> wrote in message news:63f9309a-f1c4-4e59-8e18-6185d67491ba...

    Thank you for your reply.

    I addes some code:

    public void Inspectors_NewInspector(Outlook.Inspector inspector)
    {
     var item = inspector.CurrentItem as Outlook.MailItem;
     if (item != null)
     {
      var result = OpenMailByBusinessLogic();
      if (result)
      {
       Outlook.InspectorEvents_10_Event)inspector).Activate += new Outlook.InspectorEvents_10_ActivateEventHandler(MailItem_Activate);
       item.UserProperties.Add("CloseEmail", Outlook.OlUserPropertyType.olYesNo);
       item.UserProperties["CloseEmail"].Value = true;
      }
     }
    }
    



    OpenMailByBusinessLogic returns if successfully a mail is created. If not, we need a empty mail from outlook, like always. This works fine. I added the userProperty so I can detect which email inspector I have to close in the active event:

     

    void MailItem_Activate()
    {
    	foreach (Outlook.Inspector insp in Globals.ThisAddIn.Application.Inspectors)
    	{
    		Outlook.MailItem mail = insp.CurrentItem as Outlook.MailItem;
    		var userProperty = mail.UserProperties["CloseEmail"];
    
    		if (userProperty != null && userProperty.Value)
    		{
    			mail.Display();
    			insp.Close(Outlook.OlInspectorClose.olDiscard);
    		}
    	}
    }
    
    


    if I don't call mail.Display() this code closes the wrong email.
    So, with this code everything "works".. I have just one more problem, Outlook Shows for like half second the mail windows which I close here..

     

    Any ideas?

     


    Ken Slovak MVP - Outlook
    Tuesday, July 19, 2011 2:53 PM
  • You are right. Thank you!!

    I changed my Code to this:

    public void Inspectors_NewInspector(Outlook.Inspector inspector)
    {
     var item = inspector.CurrentItem as Outlook.MailItem;
     if (item != null)
     {
      var result = OpenMailByBusinessLogic();
      if (result)
      {
       item.Open += new Outlook.ItemEvents_10_OpenEventHandler(item_Open);
      }
     }
    }
    
    void item_Open(ref bool Cancel)
    {
       Cancel = true;
    }
    

    works perfect!!

    thank you both.

    Tuesday, July 19, 2011 2:59 PM