Unable to cast COM object" error

  • Thread starter RW1yYWs
  • Start date Views 1,858
Status
Not open for further replies.
R

RW1yYWs

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!
 
R

RW1yYWs

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.
 
D

Dmitry Streblechenko

Are you sure you really have a MailItem object?

You could also have ReportItem, ContactItem, etc.

Dmitry Streblechenko (MVP)

-

"Emrak" <Emrak> wrote in message

news:041CBBF4-266F-4E53-9408-A9905EE6C75D@microsoft.com...
> 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!
 
Status
Not open for further replies.
Top