Click here to Skip to main content
15,887,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
Member 13789526 22-Dec-20 0:50am    
What is "programobj" can you please explain.

1 solution

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:
C#
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.
 
Share this answer
 
Comments
SanketAB 9-Oct-12 10:52am    
@ Thomas Duwe...
Thank you very much...it solved my problem almost immediately...Thanks again
Member 13789526 22-Dec-20 0:50am    
What is "programobj".Please explain

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900