Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Outlook
Hi all,
 
While looping through the Outlook inbox i want to test whether the current outlook item is mailitem or not. If it is, then work on it; else skip and check new entry.
This is what my code looks like
 
  
                foreach (Outlook.MailItem mailitem in programobj.folder.Items)
                {
 
                    try
                    {
 

                        if (!(mailitem is Outlook.MailItem))
                        {
                          // skip...
                            continue;
                        }
                       else
                       {
                         //work here
                        }
                  catch(exception e)
                  {
                    //some message or log
                  }
 
It works very well untill it finds any entry other than mailitem like Calendar item, note item, etc. For such items it gives me exception and loop hangs there itself
This is the Error i am getting:
 
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
 
Please help me with this. I cannot modify code much. Please give me solution where i can test whether the given item is mailitem or not with simple if(condition) else...
 
Thanks in advance!!!
Posted 9-Oct-12 2:54am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are already implicitly casting each item in programobj.folder.Items to the Outlook.MailItem type in the foreach loop header (foreach (Outlook.MailItem mailitem in...)).
 
Try this:
foreach (object item in programobj.folder.Items)
{
   try
   {
     if (item is Outlook.MailItem)
     {
       Outlook.MailItem mailitem = (Outlook.MailItem)item;
       // do something with mailitem
     }
   }
   catch(exception e)
   {
       //some message or log
   }
 
Regards,
 
Thomas.
  Permalink  
Comments
sanketbramhe at 9-Oct-12 10:52am
   
@ Thomas Duwe...
Thank you very much...it solved my problem almost immediately...Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 BillWoodruff 235
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100