I'm working on this outlook automation in visual basic and I need to dynamically figure out what type of folder (mail, contacts, tasks, etc.) the current folder is. I tried both olFolderInst.Class and olFolderInst.Items.Class and they always return the same value of olfolder and olitem respectivly regardless of what the folder is. How do I go about doing this

How to detect item class in outlook object model
kevin83
(1) The 'least code' way is to not cast the outlook type unless you have to, as this will work wherever the item shared common properties and/or methods i.e. use a 'helper' function to call the COM object with a late bound method, like this:
"string subject = (string) OLLateBoundPropertyGetHelper(cItem, "Subject");
public
object OLLateBoundPropertyGetHelper(object targetObject, string propertyName){
return(targetObject.GetType().InvokeMember(propertyName,BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.GetProperty,
null,
targetObject,
null,
CultureInfo.CurrentCulture));
}"
(2) The other way is to test the type, but without using an exception, i.e. something like this:
"GenericSaveItem(cItem);
public
void GenericSaveItem(object item){
if (item is MailItem)
{
MailItem mi = item as MailItem;
mi.Save() ;
}
else if (item is AppointmentItem)
{
AppointmentItem ai = item as AppointmentItem;
ai.Save() ;
... and so on - there are about 15 types."
For more details let me know, or grab a copy of something like 'VSTO ISBN:0-321-33488-4' ( http://tinyurl.com/qyc9c ) as it explains better than I could.
Tpzguy
Antoniodya
foreach(object item in items)
{
Outlook.ContactItem cItem = null;
try
{
cItem = (Outlook.ContactItem)item;
}
catch(Exception e){}
}
...
which seems suboptimal at best. I guess the real question is how you get the type value out of an object (not comobject :))
Thx
Marcuscoker
Something like this should work:
Outlook.OlItemType type = oYourFolder.DefaultItemType;
switch (type)
{
case Outlook.OlItemType.olAppointmentItem;
// We have an appointment
case Outlook.OlItemType.olMainItem
// We have a Mail item
}
The other approach is not to strongly type your calls and do that 'big switch cast' thing, i.e. use things like .InvokeMember late bound get/set helper functions.