none
Determining the Type of an Outlook item not working RRS feed

  • Question

  • Hi

    I'm using the ExplorerSelectionChange event and I need to check the type of Outlook item I'm dealing with.

    I can get an object from the selection and try to cast it as a MailItem, catch it if it fails and try to cast it as a ContactItem etc but that is very clumsy. Instead I tried to use the is operator as in the code below. However, it is not recognising anything.

    Can you spot what I have done wrong? Debugging shows the object is retrieved from the selection but it is not recognised as any Outlook item type. (Info is just a static class to carry references needed through the add-in.)

    private void adxOutlookAppEvents1_ExplorerSelectionChange(object sender, object explorer)
            {
                object oItem = null;
                if (Info.OutlookApplication.ActiveExplorer().Selection.Count == 1)
                {
                    oItem = Info.OutlookApplication.ActiveExplorer().Selection;
    
                    if (oItem is Outlook.AppointmentItem) MessageBox.Show("Appointment", "Outlook item type");
                    if (oItem is Outlook.ContactItem) MessageBox.Show("Contact", "Outlook item type");
                    if (oItem is Outlook.DistListItem) MessageBox.Show("Distlist", "Outlook item type");
                    if (oItem is Outlook.DocumentItem) MessageBox.Show("Document", "Outlook item type");
                    if (oItem is Outlook.JournalItem) MessageBox.Show("Journal", "Outlook item type");
                    if (oItem is Outlook.MailItem) MessageBox.Show("Mail", "Outlook item type");
                    if (oItem is Outlook.MeetingItem) MessageBox.Show("Meeting", "Outlook item type");
                    if (oItem is Outlook.NoteItem) MessageBox.Show("Note", "Outlook item type");
                    if (oItem is Outlook.PostItem) MessageBox.Show("Post", "Outlook item type");
                    if (oItem is Outlook.RemoteItem) MessageBox.Show("Remote", "Outlook item type");
                    if (oItem is Outlook.ReportItem) MessageBox.Show("Report", "Outlook item type");
                    if (oItem is Outlook.SharingItem) MessageBox.Show("Sharing", "Outlook item type");
                    if (oItem is Outlook.StorageItem) MessageBox.Show("Storage", "Outlook item type");
                    if (oItem is Outlook.TaskItem) MessageBox.Show("Task", "Outlook item type");
                    if (oItem is Outlook.TaskRequestAcceptItem) MessageBox.Show("Task Request Accept", "Outlook item type");
                    if (oItem is Outlook.TaskRequestDeclineItem) MessageBox.Show("Task Request Decline", "Outlook item type");
                    if (oItem is Outlook.TaskRequestItem) MessageBox.Show("Task Request", "Outlook item type");
                    if (oItem is Outlook.TaskRequestUpdateItem) MessageBox.Show("Task Request Update", "Outlook item type");
                }
            }


    Mike VE

    Friday, December 4, 2015 11:35 AM

Answers

All replies

  • hmm, without further information: while it often works to reference an outside variable like Info.OutlookApplication.ActiveExplorer, it may fail in more complex scenarios. Use the explorer parameter instead.

    Then - without testing - when Selection is a collection, then you should assign oItem = explorer.Selection[0].

    Friday, December 4, 2015 12:02 PM
  • Hello Mike,

    The Selection object can't be cast to Outlook items. You need to get an item first because the Selection object may contain multiple items, for example:

    Object selObject = this.Application.ActiveExplorer().Selection[1];
    if (selObject is Outlook.MailItem)
    {

    See How to: Programmatically Determine the Current Outlook Item for more information.

    Friday, December 4, 2015 12:09 PM
  • Thanks guys. Adding [1] to selection did the trick. I should have known that!

    Mike VE

    Friday, December 4, 2015 12:27 PM
  • Hi Mike,

    Please mark it as answer to close this thread if helps you to solve that issue..

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 8, 2015 5:34 AM
    Moderator