none
Outlook 2010 adding code to the mail item RRS feed

  • Question

  • can someone direct me to where I can find info about adding code so that when a user opens up an existing email, code gets triggered.

    I wrote code and called it via a ribbon button, however I need to trigger said code when a user opens up an existing email (not a new one)

    Thank you in advance to anyone to helps....

    Tuesday, August 7, 2012 1:19 PM

Answers

  • Handle the NewInspector() event of the Inspectors collection, which will fire when any item is opened. In that event handler you are passed the Inspector which is opening. Use a test like this to decide if the item is a mail item:
     
    if (Inspector.CurrentItem is Outlook.MailItem)
     
    The set up to handle the Activate() event of the Inspector. When that fires you have a strong object reference to the Inspector and its CurrentItem with all properties filled in. Before that it's a weak object reference with most of the properties null.
     
    When you get the strong reference check for the item's EntryID. If (!String.IsNullOrEmpty(item.EntryID)) you have an existing item. If EntryID is null or null string, it's a new item that has never been saved. EntryID is set when the item is first saved.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:1b8d40ef-c0fd-4500-ba49-f76d57b3ef6d...

    can someone direct me to where I can find info about adding code so that when a user opens up an existing email, code gets triggered.

    I wrote code and called it via a ribbon button, however I need to trigger said code when a user opens up an existing email (not a new one)

    Thank you in advance to anyone to helps....


    Ken Slovak MVP - Outlook
    Tuesday, August 7, 2012 2:35 PM
    Moderator
  • This doesn't take into account that more than 1 Inspector can be open at one time, and doesn't use a wrapper class.
     
    // at class level
    Outlook.MailItem _mail = null;
    Outlook.Inspector _insp = null;

    Outlook.ItemEvents_Event _itemEvents = null;

    //In NewInspector():
     
    if (Inspector.CurrentIem is Outlook.MailItem)
    {
        _insp = Inspector;
        _mail = Inspector.CurrentItem;
     

         ((Outlook.InspectorEvents_Event)_insp).Close += new Outlook.InspectorEvents_CloseEventHandler(InspClose);

        ((Outlook.InspectorEvents_Event)_insp).Activate += new Outlook.InspectorEvents_ActivateEventHandler(InspActivate);

        _itemEvents = (Outlook.ItemEvents_Event)_mail;

        // hook up the MailItem Close and Open event handlers

        _itemEvents.Close += new Outlook.ItemEvents_CloseEventHandler(OnItem_Close);

        _itemEvents.Open += new Outlook.ItemEvents_OpenEventHandler(OnItem_Open);

     

    void OnItem_Open(ref bool Cancel)

    {

    }

    OnItem_Close(ref bool Cancel)

    {

        if (needToClose)

        {

            Cancel = true;

        }

    }

    A wrapper class sets up all the handlers and basic code and contains instances of Inspector and MailItem (or other types). You then add each wrapper to a list/dictionary/whatever to keep it alive, you remove the wrapper from the list when the item is closed or sent.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:c21d7bd6-dc08-4786-8c50-c2959994f7eb...

    could you provide some sample code?

    I am not having luck finding sample code on my own


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 5:26 PM
    Moderator
  • MailItem.Open() has a Cancel argument, set it to false.
     
    What I do in this circumstance is to set up to handle the Inspector and Inspector.CurrentItem events I want in NewInspector(). When the Open() event fires on the item is when I can close it or deal with anything related to an open item.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:056420f8-0b09-4a28-96cf-4995155c11dc...

    this is working now, thank you....

    One last question, using the above code, how do I stop an email from opening?

    basically, I have code in place that is asking the user if they wish to continue.

    if they click OK, the email opens as it should

    if they click Cancel, I need to stop the email from opening

    so how do I stop the email from opening


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 2:55 PM
    Moderator
  • // at class level

    Outlook.ItemEvents_Event _itemEvents = null;

    void InspActivate()

    {

    }

    void InspClose()

    {

    }

    Intellisense would fill in and create those event handlers for you if you aren't just doing a copy and paste.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007

    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:e720cc15-2402-4849-ae88-7234f5b952b8...

    In your code sample, the below items are not defined, what should they be defined as?

    InspClose

    InspActivate

    _itemEvents


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 7:57 PM
    Moderator
  • OK, I think I figured out what we were doing wrong, we were trying to work on the close event,

    we should have been working on the open event, once I added the Cancel = true to the open event method (OnItem_Open) then it worked as I wished it to

    Thank you for your time and efforts....

    • Marked as answer by niyack Thursday, August 9, 2012 11:53 AM
    Thursday, August 9, 2012 11:53 AM

All replies

  • Handle the NewInspector() event of the Inspectors collection, which will fire when any item is opened. In that event handler you are passed the Inspector which is opening. Use a test like this to decide if the item is a mail item:
     
    if (Inspector.CurrentItem is Outlook.MailItem)
     
    The set up to handle the Activate() event of the Inspector. When that fires you have a strong object reference to the Inspector and its CurrentItem with all properties filled in. Before that it's a weak object reference with most of the properties null.
     
    When you get the strong reference check for the item's EntryID. If (!String.IsNullOrEmpty(item.EntryID)) you have an existing item. If EntryID is null or null string, it's a new item that has never been saved. EntryID is set when the item is first saved.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:1b8d40ef-c0fd-4500-ba49-f76d57b3ef6d...

    can someone direct me to where I can find info about adding code so that when a user opens up an existing email, code gets triggered.

    I wrote code and called it via a ribbon button, however I need to trigger said code when a user opens up an existing email (not a new one)

    Thank you in advance to anyone to helps....


    Ken Slovak MVP - Outlook
    Tuesday, August 7, 2012 2:35 PM
    Moderator
  • is there a book that I can buy what would tell me all of this?

    (rather than pester others for the info)

    Tuesday, August 7, 2012 3:59 PM
  • The first place to look is http://www.outlookcode.com/, that has a large amount of code and lots of informative articles on Outlook coding.
     
    A list of Outlook programming books is at http://www.outlookcode.com/article.aspx?ID=2

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:208114d1-a280-4c87-987f-8595b901332a...

    is there a book that I can buy what would tell me all of this?

    (rather than pester others for the info)


    Ken Slovak MVP - Outlook
    Tuesday, August 7, 2012 4:57 PM
    Moderator
  • your suggestion worked perfectly, thank you.

    I am getting an error in my code now but I was not getting this error when the code was called from a ribbon.

    I have tracked down where the error is coming from, the error trips when it gets to:

    mail = (Outlook.MailItem)application.ActiveInspector().CurrentItem;

    The error is:

    Object reference not set to an instance of an object

    Is there another way that I should write the code to get a hook on the current email that is being opened?

    Wednesday, August 8, 2012 1:25 PM
  • NewInspector event gives you inspector object as a parameter. Use this one.

    Wednesday, August 8, 2012 1:41 PM
  • Depending on when you use ActiveInspector() you might get null, or you might get an unexpected Inspector. For example, if there is an item open already and another is opened, ActiveInspector() would be the fitst item until the Activate() event fires on the second item.
     
    If you are handling NewInspector() use the Inspector reference from there, as Damian said. But don't expect the properties to all be filled in until Activate() fires on the Inspector. For other places in the code, if ActiveInspector() is null you can check Inspectors.Count and if Count == 1 you can always use Inspectors[1].

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:2ccc5dec-225a-4491-a46e-5d55abc8d65b...

    your suggestion worked perfectly, thank you.

    I am getting an error in my code now but I was not getting this error when the code was called from a ribbon.

    I have tracked down where the error is coming from, the error trips when it gets to:

    mail = (Outlook.MailItem)application.ActiveInspector().CurrentItem;

    The error is:

    Object reference not set to an instance of an object

    Is there another way that I should write the code to get a hook on the current email that is being opened?


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 2:29 PM
    Moderator
  • this is working now, thank you....

    One last question, using the above code, how do I stop an email from opening?

    basically, I have code in place that is asking the user if they wish to continue.

    if they click OK, the email opens as it should

    if they click Cancel, I need to stop the email from opening

    so how do I stop the email from opening

    Wednesday, August 8, 2012 2:45 PM
  • MailItem.Open() has a Cancel argument, set it to false.
     
    What I do in this circumstance is to set up to handle the Inspector and Inspector.CurrentItem events I want in NewInspector(). When the Open() event fires on the item is when I can close it or deal with anything related to an open item.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:056420f8-0b09-4a28-96cf-4995155c11dc...

    this is working now, thank you....

    One last question, using the above code, how do I stop an email from opening?

    basically, I have code in place that is asking the user if they wish to continue.

    if they click OK, the email opens as it should

    if they click Cancel, I need to stop the email from opening

    so how do I stop the email from opening


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 2:55 PM
    Moderator
  • could you provide some sample code?

    I am not having luck finding sample code on my own

    Wednesday, August 8, 2012 3:47 PM
  • This doesn't take into account that more than 1 Inspector can be open at one time, and doesn't use a wrapper class.
     
    // at class level
    Outlook.MailItem _mail = null;
    Outlook.Inspector _insp = null;

    Outlook.ItemEvents_Event _itemEvents = null;

    //In NewInspector():
     
    if (Inspector.CurrentIem is Outlook.MailItem)
    {
        _insp = Inspector;
        _mail = Inspector.CurrentItem;
     

         ((Outlook.InspectorEvents_Event)_insp).Close += new Outlook.InspectorEvents_CloseEventHandler(InspClose);

        ((Outlook.InspectorEvents_Event)_insp).Activate += new Outlook.InspectorEvents_ActivateEventHandler(InspActivate);

        _itemEvents = (Outlook.ItemEvents_Event)_mail;

        // hook up the MailItem Close and Open event handlers

        _itemEvents.Close += new Outlook.ItemEvents_CloseEventHandler(OnItem_Close);

        _itemEvents.Open += new Outlook.ItemEvents_OpenEventHandler(OnItem_Open);

     

    void OnItem_Open(ref bool Cancel)

    {

    }

    OnItem_Close(ref bool Cancel)

    {

        if (needToClose)

        {

            Cancel = true;

        }

    }

    A wrapper class sets up all the handlers and basic code and contains instances of Inspector and MailItem (or other types). You then add each wrapper to a list/dictionary/whatever to keep it alive, you remove the wrapper from the list when the item is closed or sent.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:c21d7bd6-dc08-4786-8c50-c2959994f7eb...

    could you provide some sample code?

    I am not having luck finding sample code on my own


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 5:26 PM
    Moderator
  • In your code sample, the below items are not defined, what should they be defined as?

    InspClose

    InspActivate

    _itemEvents

    Wednesday, August 8, 2012 7:39 PM
  • // at class level

    Outlook.ItemEvents_Event _itemEvents = null;

    void InspActivate()

    {

    }

    void InspClose()

    {

    }

    Intellisense would fill in and create those event handlers for you if you aren't just doing a copy and paste.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007

    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:e720cc15-2402-4849-ae88-7234f5b952b8...

    In your code sample, the below items are not defined, what should they be defined as?

    InspClose

    InspActivate

    _itemEvents


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 7:57 PM
    Moderator
  • ok, I got that figured out, however a Cancel = true; only stops the user from closing the email.

    I am looking for the code to close the email that the user is currently in

    Wednesday, August 8, 2012 8:04 PM
  • So if you have a reference to the item or Inspector, just call the Close() method with whatever argument you want to supply.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:c0d7d5d4-5562-4a93-8d0b-fd801aa5460a...

    ok, I got that figured out, however a Cancel = true; only stops the user from closing the email.

    I am looking for the code to close the email that the user is currently in


    Ken Slovak MVP - Outlook
    Wednesday, August 8, 2012 9:06 PM
    Moderator
  • I tried that, the mailItem.Close gets ignored

    No matter what I try, the mail doc opens, hence why I came here for help

    Thursday, August 9, 2012 11:42 AM
  • OK, I think I figured out what we were doing wrong, we were trying to work on the close event,

    we should have been working on the open event, once I added the Cancel = true to the open event method (OnItem_Open) then it worked as I wished it to

    Thank you for your time and efforts....

    • Marked as answer by niyack Thursday, August 9, 2012 11:53 AM
    Thursday, August 9, 2012 11:53 AM
  • Isn't that what I said originally, set Cancel == true in the item.Open() event?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "niyack" <=?utf-8?B?bml5YWNr?=> wrote in message news:079dd12b-60e0-45c3-9ce7-93340324915c...

    OK, I think I figured out what we were doing wrong, we were trying to work on the close event,

    we should have been working on the open event, once I added the Cancel = true to the open event method (OnItem_Open) then it worked as I wished it to

    Thank you for your time and efforts....


    Ken Slovak MVP - Outlook
    Thursday, August 9, 2012 3:24 PM
    Moderator
  • Looking back at this thread, I see that now,it looks like I got confused when in your sample code it is in the close event

    no worries, it is resolved now, many thanks....

    Thursday, August 9, 2012 4:39 PM