Unable to cast COM object" error

Status
Not open for further replies.
R

RW1yYWs

Hey all, I receive the following error:

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!
 
Don't multipost. Also, always post your Outlook version.

You need to do a logon to NameSpace if you are using this code in a

standalone appliation. If it's a COM addin use the application object passed

to you in your connection handler. You also need to refer to the Public

Folders tree correctly.

object _missing = System.Reflection.Missing.Value;

oOutlook = new Outlook.Application();

oNs = oOutlook.GetNamespace("MAPI");

oNs.Logon(_missing, _missing, _missing, _missing);

oFldr =

oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders

).Folders["Test"];

for (int i = 1; i <= oFldr.Items.Count; i++)

{

Object o = oFldr.Items;

object[] args = new Object[] { };

Type t = o.GetType();

object retVal = o.InvokeMember("Class", BindingFlags.Public |

BindingFlags.GetField |

BindingFlags.GetProperty, null, o, args);

Outlook.OlObjectClass itemClass = (Outlook.OlObjectClass)retVal;

if (itemClass == Outlook.OlObjectClass.olNote)

{

// it's a mail item

Outlook.MailItem oMail = (Outlook.MailItem)o;

}

}

There are plenty of Outlook code samples in C# at www.outlookcode.com, my

Web site, the MS Office Developer Web site and many other places. It might

be helpful to you to study some of the code to see how things are done.

What I showed has no error handling, something especially needed in managed

code.

"Emrak" <Emrak> wrote in message

news:568EA84A-0B4A-4C13-AB07-9E0D218B1296@microsoft.com...
> Hey all, I receive the following error:
> 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!
 
Good day Mr Slovak,

My apologies for the multipost. Desperation isn't just an 11-letter word.

I have most definitely explored many, many of the programmatic solutions

available online.

My Outlook version is 2003, SP3.

I appreciate your code sample. My main problem at this point (what it has

always been, actually) is not so much handling the "MailItem" objects, but

handling the "discussion" objects. How do I reference them, specifically?

Olnote references only MailItems and not discussions. The bulk of email

received is in a "discussion" format.

Thanks much for your assistance.
 
As an addendum to my last post, I've learned that "Post" is the object type

I'm looking for. As such, I'll demonstrate my current solution for posterity.

I'm using the KISS method here. This works, but I can't determine how, in the

context of "string filter" below, how I'm supposed to code "OR" below. For

instance, MessageClass = NOTE OR POST.

//instanciate variables

Microsoft.Office.Interop.Outlook.Application oOutlook;

Microsoft.Office.Interop.Outlook.NameSpace oNs;

Microsoft.Office.Interop.Outlook.MAPIFolder oFldr;

int iAttachCnt = 0;

oOutlook = new Microsoft.Office.Interop.Outlook.Application();

oNs = oOutlook.GetNamespace("MAPI");

//are there any emails to process?

oFldr = oNs.Folders["Public Folders"].Folders["Test"]

if (oFldr.Items.Count > 0)

{

//cycle through each email, filtering out only messages and posts, no

calendars or tasks

string filter = "[MessageClass] = \"IPM.Note\"";

Microsoft.Office.Interop.Outlook.Items oTestItems =

oFldr.Items.Restrict(filter);

> ...

}
 
I am making the assumption as of now (tell me if I'm wrong), that I can only

process Posts and Notes separately. I thank you for your time.
 
You can filter or restrict on both Note and Post items in the same filter.

Obviously when you retrieve an Object that could be either you further need

to test for either Class or MessageClass to know which type to assign that

Object.

However, unless you don't have any possibility of running into custom forms

or want to exclude all custom forms you are probably best off using a filter

that checks for the MessageClass starting with "IPM.Note" or starting with

"IPM.Post". That's easiest to do using the undocumented capability of using

SQL type statements in a filter.

That filter would look something like this. The DASL property tags aren't

URL's, they are actually the DASL equivalent of "MessageClass".

"http://schemas.microsoft.com/mapi/proptag/0x001a001e" LIKE 'IPM.Note%' OR

"http://schemas.microsoft.com/mapi/proptag/0x001a001e" LIKE 'IPM.Post%'

I'd construct that using StringBuilder myself probably:

StringBuilder sb = new StringBuilder();

sb.Append(@"http://schemas.microsoft.com/mapi/proptag/0x001a001e" + @" LIKE

'IPM.Note%' OR" + @"http://schemas.microsoft.com/mapi/proptag/0x001a001e" +

@" LIKE 'IPM.Post%'");

string filter = "@SQL=" + sb.ToString();

Microsoft.Office.Interop.Outlook.Items oTestItems =

oFldr.Items.Restrict(filter);

"Emrak" <Emrak> wrote in message

news:5DC7B176-4168-4A02-ADE6-C26B220D19CC@microsoft.com...
> I am making the assumption as of now (tell me if I'm wrong), that I can
> only
> process Posts and Notes separately. I thank you for your time.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
R Unable to cast COM object" error Outlook VBA and Custom Forms 2
C Unable to cast object of type 'System.Int32' to type 'System.Byte[ BCM (Business Contact Manager) 1
D Unable to change AppointmentItem.Start property Outlook VBA and Custom Forms 3
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Unable to Use Alias as From Address Using Outlook 2
R unable to filter tasks when start date is on or before today Using Outlook 3
J Unable to delete folders in Outlook 2019 / Windows Using Outlook 0
D Unable to view older emails in desktop app Using Outlook 0
M Issues with templates - unable to find in old web app Using Outlook 3
Commodore Unable to move message Using Outlook 3
S Unable to change Message Class Outlook VBA and Custom Forms 0
O Unable to make changes to an existing Calendar entry Using Outlook 11
C Outlook 2016 Unable to delete Gmail IMAP calendar Using Outlook 2
S Unable to extract text from an Outlook email message Using Outlook 2
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
M Unable to share 2019 calendar Using Outlook 0
D Outlook 2016 Unable to load Outlook data pst file Using Outlook 5
L Unable to Sync Web/Android MS To Do with Windows Outlook Tasks Using Outlook 3
A Unable to save recurring Meeting to Documents folder due to error Using Outlook 2
O Outlook 365 - suddenly unable to send using Gmail POP3 Using Outlook 10
S Outlook 2010 unable to change default font Using Outlook 7
Q Unable to Sync Quicken reminder with Outlook 2016 64Bit Using Outlook 1
S Unable to remove rule outlook 2010 Using Outlook 0
L Wierd Office 365 Contact unable to edit body of random contacts Using Outlook 5
X Unable to send an email from one account to another on same PC Using Outlook 2
Mark Foley Unable to subscribe to published calendar in Outlook 2010 Using Outlook 4
S Unable to Edit Contact Information in Certain Contact Folders Using Outlook 3
P Deleted Items - Unable to Iterate All of Items Outlook VBA and Custom Forms 1
G Unable to dismiss reminders from share point list calendar shared in Outlook Using Outlook 2
R Unable to install without email account Using Outlook 4
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
A Outlook 2016- unable to have all subfolders expanded when opening outlook Using Outlook 11
avant-guvnor Unable to view certain emails in Outlook 2016 Using Outlook 16
M Unable to Configure Gmail Account in Outlook 2007 Using Outlook 1
D Unable to Send On Behalf of Another User Exchange Server Administration 0
J Unable to link email messages in BCM using a single microsoft office 365 account in outlook 2013 BCM (Business Contact Manager) 1
Tim King Outlook 365 unable to change accounts Using Outlook 0
R Unable to send mail using Cox.net Using Outlook 1
T Unable to 'Upload a file' using popup 'Browse' button Using Outlook 0
D Unable to add email address to contact Using Outlook 3
G Outlook calendar entry corrupted. Constant pop up when you open outlook. Unable to delete or remove. Using Outlook 2
O OL2000: Unable to create IMAP account Using Outlook 2
wallisellener Unable to reply/post/create new thread using Chrome BCM (Business Contact Manager) 5
Rupert Dragwater unable to change font sizes in some replies Using Outlook 3
C Unable to see meeting attendees Outlook 2010 Using Outlook 5
M Unable to email from Word or Excel Using Outlook 11
O Unable to check name. Using Outlook 3
T Unable to create contacts subfolder in EAS profile Using Outlook.com accounts in Outlook 6
UncleBill Unable to delete items from gmail IMAP Trash using Outlook 2010 Outlook VBA and Custom Forms 5
B Unable to search delegated mailfile Using Outlook 3

Similar threads

Back
Top