Listing an Outlook Mailbox Using Visual Studio

I have the following class, which is designed to return the subject line of all letters in a folder

This is Visual Studio 2008 vs Outlook 2007, running on Windows 7 64bit

using System; using System.Windows; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Outlook; namespace MarketingEmails { public class MailUtils { public static string[] processMailMessages(object outlookFolder) // Given an Outlook folder as an object reference, return // a list of all the email subjects in that folder { // Set a local object from the folder passed in Folder theMailFolder = (Folder)outlookFolder; string[] listSubjects = new string[theMailFolder.Items.Count]; int itemCount = 0; // Now process the MAIL items foreach (MailItem oItem in theMailFolder.Items) { listSubjects[itemCount] = oItem.Subject.ToString(); itemCount++; } return listSubjects; } } 

}

However, the code throws an exception below:

Cannot pass a COM object of type "System .__ ComObject" to the interface type "Microsoft.Office.Interop.Outlook.MailItem". This operation failed because the call to QueryInterface on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046} failed due to the following error: this interface is not supported (exception from HRESULT: 0x80004002 (E_NOINTERFACE) )

I understand that an error has occurred because it is trying to process the ReportItem in the selected mailbox.

What I don’t understand is why I’m trying to execute the process of processing non-Mail items when I pointed out:

 foreach (MailItem oItem in theMailFolder.Items) 

If I wanted him to process the entries in the entries in the mailbox, I would write:

 foreach (ReportItem oItem in theMailFolder.Items) 

I would love to know if this is a mistake or is it just my misunderstanding

Regards, Nigel Ainsko

+6
c # visual-studio-2008 outlook
source share
2 answers

The reason the collection items contain instances of MailItem and ReportItem. Specifying MailItem on the left does not filter the list; it simply indicates what type you expect to be in the list.

What you need to do is filter by the type you need, thus

 foreach ( MailItem oItem in theMailFolder.Items.OfType<MailItem>()) { .. } 

The OfType method will only return values ​​in the collection that match this particular type.

+8
source share

A type declaration in a foreach does not filter by type - instead, it throws an exception, as you noticed.

It does this because foreach was introduced in C # 1.0, which did not support generics. Therefore, the compiler does not know which type is returned by IEnumerator . (This is still true if the collection does not implement IEnumerable<T> ). Nitpickers: I know that it is possible even in C # 1 to write a strongly typed counter (for example, List<T> ); the vast majority are not.

Then, if you accidentally put the wrong type in the foreach , you would prefer it to throw an exception rather than mysteriously do nothing.

As JaredPar explained, you should use the OfType method.

+2
source share

All Articles