A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Add-ins for Outlook
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Unable to cast COM object" error



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old April 29th 09, 06:36 PM posted to microsoft.public.outlook.program_addins
Emrak
external usenet poster
 
Posts: 6
Default Unable to cast COM object" error

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[i];
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[i]"
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!
Ads
  #2  
Old April 29th 09, 11:26 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Unable to cast COM object" error

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.olPu blicFoldersAllPublicFolders
).Folders["Test"];

for (int i = 1; i = oFldr.Items.Count; i++)
{
Object o = oFldr.Items[i];
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Emrak" wrote in message
...[i]
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[i];
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!


  #3  
Old April 30th 09, 04:21 PM posted to microsoft.public.outlook.program_addins
Emrak
external usenet poster
 
Posts: 6
Default Unable to cast COM object" error

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.
  #4  
Old April 30th 09, 04:39 PM posted to microsoft.public.outlook.program_addins
Emrak
external usenet poster
 
Posts: 6
Default Unable to cast COM object" error

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);
....
}
  #5  
Old April 30th 09, 04:53 PM posted to microsoft.public.outlook.program_addins
Emrak
external usenet poster
 
Posts: 6
Default Unable to cast COM object" error

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.
  #6  
Old April 30th 09, 07:01 PM posted to microsoft.public.outlook.program_addins
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Unable to cast COM object" error

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();

/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);

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Emrak" wrote in message
...
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.


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast COM object" error Emrak Outlook - Using Forms 2 May 1st 09 07:49 AM
Unable to cast object of type 'System.__ComObject' to type 'Microsoft.Office.Interop.Outlook.ApplicationClass' John Yovas Add-ins for Outlook 2 September 12th 08 04:23 PM
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.ContactItem'. Dhananjay Outlook and VBA 3 October 13th 07 07:56 AM
Unable to reply "null or not an object" Dave Outlook Express 1 November 13th 06 07:04 PM
“The operation failed. An object could not be found. Unable to..." Milton Outlook - Calandaring 0 August 3rd 06 04:07 PM


All times are GMT +1. The time now is 08:43 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.