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!