none
Outlook 2007 Item get type help RRS feed

  • Question

  • Hello,

    I would like some help determining what type an item is within an outlook folder. I have been looking at several different threads that are close to this but not enough information to solve my individual need.

    I prefer not to do the following in C# becasue for every type I would need an if statement and return.

    Outlook.Items fItems = folder.Items;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {
          System.Diagnostics.Debug.WriteLine("MailItem");
       }
                            
    }

    Wednesday, August 10, 2011 3:32 PM

Answers

  • Either as before:
        mi= fItems[i] as Outlook.MailItem;
     
    or using a cast operator
        mi= (Outlook.MailItem)fItems[i];
     
    or any of the other cast operations. It doesn't matter. If you test for the item type you can avoid having to handle an exception if you cast to the wrong type, that's about it. It's all a matter of how you want to handle cases where the cast is not appropriate: checking type before a cast, or handling an exception as a result of a cast.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:7fabaddc-9114-4a18-8171-81f9c3b66f37...

    Ken,

    Thanks for the help, so how would I go about casting the item to the correct type after that so I can you the move() method?

    Using my previous code example if you don't mind.

    Outlook.Items fItems = folder.Items;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

          Cast the item here and move it... please help.
       }
                            
    }

    I figured it out thanks,

    New code below if anyone else is looking for it

    Outlook.Items fItems = folder.Items;

    Outlook.MailItem mi = null;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

           mi= fItems[i] as Outlook.MailItem;

           mi.Move(DestinationFolder);
       }
                            
    }


    Ken Slovak MVP - Outlook
    • Marked as answer by Ed.A Wednesday, August 10, 2011 6:34 PM
    Wednesday, August 10, 2011 6:22 PM
  • "IPM.Note" is the MessageClass for MailItem. "IPM.StickyNote" is the MessageClass for NoteItem (notes).

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:6d11beeb-8c35-4ecc-bf52-4b3656e44ef8...

    Ken,

    I edited the above post as you were replying. MFCMAPI shows the following in formation for the message.

    PR_MESSAGE_CLASS = IPM.NOTE

    But in the above code I am checking if it is a NoteItem and it is not firing.


    Ken Slovak MVP - Outlook
    • Marked as answer by Ed.A Thursday, August 11, 2011 3:54 PM
    Thursday, August 11, 2011 3:17 PM

All replies

  • No matter what you still need to test each item unless you filter or restrict on MessageClass. You can't use Class in a filter or restriction.
     
    I usually test for item.Class to see what it is, or MessageClass.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:44ffcf2d-de9a-4e7b-a4f2-bd624b90f8f0...

    Hello,

    I would like some help determining what type an item is within an outlook folder. I have been looking at several different threads that are close to this but not enough information to solve my individual need.

    I prefer not to do the following in C# becasue for every type I would need an if statement and return.

    Outlook.Items fItems = folder.Items;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {
          System.Diagnostics.Debug.WriteLine("MailItem");
       }
                            
    }


    Ken Slovak MVP - Outlook
    Wednesday, August 10, 2011 3:41 PM
  • Ken,

    Thanks for the help, so how would I go about casting the item to the correct type after that so I can you the move() method?

    Using my previous code example if you don't mind.

    Outlook.Items fItems = folder.Items;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

          Cast the item here and move it... please help.
       }
                            
    }

    I figured it out thanks,

    New code below if anyone else is looking for it

    Outlook.Items fItems = folder.Items;

    Outlook.MailItem mi = null;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

           mi= fItems[i] as Outlook.MailItem;

           mi.Move(DestinationFolder);
       }
                            
    }

    Wednesday, August 10, 2011 5:45 PM
  • Either as before:
        mi= fItems[i] as Outlook.MailItem;
     
    or using a cast operator
        mi= (Outlook.MailItem)fItems[i];
     
    or any of the other cast operations. It doesn't matter. If you test for the item type you can avoid having to handle an exception if you cast to the wrong type, that's about it. It's all a matter of how you want to handle cases where the cast is not appropriate: checking type before a cast, or handling an exception as a result of a cast.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:7fabaddc-9114-4a18-8171-81f9c3b66f37...

    Ken,

    Thanks for the help, so how would I go about casting the item to the correct type after that so I can you the move() method?

    Using my previous code example if you don't mind.

    Outlook.Items fItems = folder.Items;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

          Cast the item here and move it... please help.
       }
                            
    }

    I figured it out thanks,

    New code below if anyone else is looking for it

    Outlook.Items fItems = folder.Items;

    Outlook.MailItem mi = null;
                       
    for (int i = 1; i < fItems.Count; i++)

    {
        if (fItems[i] is Outlook.MailItem)
       {

           mi= fItems[i] as Outlook.MailItem;

           mi.Move(DestinationFolder);
       }
                            
    }


    Ken Slovak MVP - Outlook
    • Marked as answer by Ed.A Wednesday, August 10, 2011 6:34 PM
    Wednesday, August 10, 2011 6:22 PM
  • Ken,

    Sorry to revisit this, but when processing through a folder that has SharePoint subsciption notifications in it I cannot determine what type of item this is. Any ideas?

    Thanks,

    Wednesday, August 10, 2011 8:32 PM
  • How are you doing your checking, are you using MessageClass or Class?
     
    I haven't done any work with Sharepoint items like that in years, they ight be RSS messages, but it's easy to figure it out. Select one of the Sharepoint items in an Outlook folder view. Open the VBA project (Alt+F11). Open the Immediate window from the View menu if it's not already open.
     
    In the Immediate window type this and then prress Enter:
     
    ? Application.ActiveExplorer.Selection.Item(1).Class
     
    That will print a number from the OlObjectClass enum that can be looked up in the Object Browser.
     
    Then in the Immediate window type this and press Entrer to get the MessageClass:
     
    ? Application.ActiveExplorer.Selection.Item(1).MessageClass

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:b11369dd-254c-47f9-b66d-897cbf2919ae...

    Ken,

    Sorry to revisit this, but when processing through a folder that has SharePoint subsciption notifications in it I cannot determine what type of item this is. Any ideas?

    Thanks,


    Ken Slovak MVP - Outlook
    Thursday, August 11, 2011 1:07 PM
  • I hate to sound stupid but I am not sure.... I tried your VBA code and the first line returns (43) and the second line throws this error( Object required)Currently I am using the following code to check the object type and then take action based on it's type.

    Outlook.Items fItems = folder.Items;
                       
                        for (int i = 1; i < fItems.Count; i++)
                        {
                           
                            if (fItems[i] is Outlook.MailItem)
                            {
                                System.Diagnostics.Debug.WriteLine("MailItem");
                            }
                            else if (fItems[i] is Outlook.PostItem)
                            {
                                System.Diagnostics.Debug.WriteLine("PostItem");
                            }
                            else if (fItems[i] is Outlook.ReportItem)
                            {
                                System.Diagnostics.Debug.WriteLine("ReportItem");
                            }
                            else if (fItems[i] is Outlook.AppointmentItem)
                            {
                                System.Diagnostics.Debug.WriteLine("AppointmentItem");
                            }
                            else if (fItems[i] is Outlook.ContactItem)
                            {
                                System.Diagnostics.Debug.WriteLine("ContactItem");
                            }
                            else if (fItems[i] is Outlook.DistListItem)
                            {
                                System.Diagnostics.Debug.WriteLine("DistListItem");
                            }
                            else if (fItems[i] is Outlook.DocumentItem)
                            {
                                System.Diagnostics.Debug.WriteLine("DocumentItem");
                            }
                            else if (fItems[i] is Outlook.NoteItem)
                            {
                                System.Diagnostics.Debug.WriteLine("NoteItem");
                            }
                            else if (fItems[i] is Outlook.TaskItem)
                            {
                                System.Diagnostics.Debug.WriteLine("TaskItem");
                            }
                        }

    None of these fire with these SharePoint alert notificatins. I tried to make sure that every item type is covered here so I could pass the oject and return it's type.


    • Edited by Ed.A Thursday, August 11, 2011 2:02 PM
    Thursday, August 11, 2011 1:57 PM
  • Do you have OutlookSpy (www.dimastr.com/outspy) or MFCMAPI from MS? Those are MAPI viewers that can be used to examine properties of items. That would help to see what's going on.
     
    Which VBA code fired the exception? The code for Class or MessageClass? Every item in Outlook should support both properties.
     
    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:34e8b605-704f-490c-96a8-1beabefcc643...

    I hate to sound stupid but I am not sure.... I tried your VBA code and it throughs and exception stating "the object doesn't support this method".

    Currently I am using the following code to check the object type and then take action based on it's type.

    Outlook.Items fItems = folder.Items;
                       
                        for (int i = 1; i < fItems.Count; i++)
                        {
                           
                            if (fItems[i] is Outlook.MailItem)
                            {
                                System.Diagnostics.Debug.WriteLine("MailItem");
                            }
                            else if (fItems[i] is Outlook.PostItem)
                            {
                                System.Diagnostics.Debug.WriteLine("PostItem");
                            }
                            else if (fItems[i] is Outlook.ReportItem)
                            {
                                System.Diagnostics.Debug.WriteLine("ReportItem");
                            }
                            else if (fItems[i] is Outlook.AppointmentItem)
                            {
                                System.Diagnostics.Debug.WriteLine("AppointmentItem");
                            }
                            else if (fItems[i] is Outlook.ContactItem)
                            {
                                System.Diagnostics.Debug.WriteLine("ContactItem");
                            }
                            else if (fItems[i] is Outlook.DistListItem)
                            {
                                System.Diagnostics.Debug.WriteLine("DistListItem");
                            }
                            else if (fItems[i] is Outlook.DocumentItem)
                            {
                                System.Diagnostics.Debug.WriteLine("DocumentItem");
                            }
                            else if (fItems[i] is Outlook.NoteItem)
                            {
                                System.Diagnostics.Debug.WriteLine("NoteItem");
                            }
                            else if (fItems[i] is Outlook.TaskItem)
                            {
                                System.Diagnostics.Debug.WriteLine("TaskItem");
                            }
                        }

    None of these fire with these SharePoint alert notificatins. I tried to make sure that every item type is covered here so I could pass the oject and return it's type.


    Ken Slovak MVP - Outlook
    Thursday, August 11, 2011 2:02 PM
  • Ken,

    I edited the above post as you were replying. MFCMAPI shows the following in formation for the message.

    PR_MESSAGE_CLASS = IPM.NOTE

    But in the above code I am checking if it is a NoteItem and it is not firing.

    Thursday, August 11, 2011 2:16 PM
  • "IPM.Note" is the MessageClass for MailItem. "IPM.StickyNote" is the MessageClass for NoteItem (notes).

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Ed.A" <=?utf-8?B?RWQuQQ==?=> wrote in message news:6d11beeb-8c35-4ecc-bf52-4b3656e44ef8...

    Ken,

    I edited the above post as you were replying. MFCMAPI shows the following in formation for the message.

    PR_MESSAGE_CLASS = IPM.NOTE

    But in the above code I am checking if it is a NoteItem and it is not firing.


    Ken Slovak MVP - Outlook
    • Marked as answer by Ed.A Thursday, August 11, 2011 3:54 PM
    Thursday, August 11, 2011 3:17 PM
  • Ken,

    Using MFCMAPI I was able to determine that for whatever reason SharePoint is sending to different message classes.  The first is a standard MailItem, the second is a SharingItem which is the items I was missing. This helped a bunch.... Thanks,

    Thursday, August 11, 2011 3:54 PM