Unable to cast COM object" error

  • Thread starter Thread starter Emrak
  • Start date Start date
E

Emrak

Hey all, I receive the following error when I try to iterate through and
access emails and their associated attachments:
Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.Office.Interop.Outlook._MailItem'.

I will demonstrate the 3 main attempts (of the trillion I've tried). :-/

Attempt #1:
oOutlook = new Outlook.Application();
oNs = oOutlook.GetNamespace("MAPI");
oFldr = oNs.Folders["Public Folders"].Folders["Test"]

foreach (MailItem oMessage in oFldr.Items)
{
....
}

Attempt #2:
oOutlook = new Outlook.Application();
oNs = oOutlook.GetNamespace("MAPI");
oFldr = oNs.Folders["Public Folders"].Folders["Test"]

foreach (Object oMessage in oFldr.Items)
{
....
}

Attempt #3:
oOutlook = new Outlook.Application();
oNs = oOutlook.GetNamespace("MAPI");
oFldr = oNs.Folders["Public Folders"].Folders["Test"]

for (int i = 1; i <= oFldr.Items.Count; i++)
{
Object o = oFldr.Items;
Type t = o.GetType();
}

I know that people are wont to send in email types other than MailItem. For
instance, there are several "discussion" items in the email box in question.
I've tried to isolate those, but when I run #3, I find that "oFldr.Items"
has a type of "System.__ComObject" which is unhelpful to me. All I'm trying
to do is grab email messages and discussion items and process them.
Unfortunately, it bombs out.

Help!
 
The main issue here is I'm unable to reach the mail items and discussion
items. I just can't. Any example code would be helpful.
 
Are you sure you really have a MailItem object?
You could also have ReportItem, ContactItem, etc.

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
Back
Top