index > Visual Studio Tools for Office > How to detect item class in outlook object model

How to detect item class in outlook object model

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?
Siggi Bjarnason

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.




David
David Ing
Thanks. I'm writing in VB but your answer put me on the right track and enabled me to solve my issue. Appreciate your help.
Siggi Bjarnason
How do you do this in the Contacts folder? It contains both contacts and distributions lists. Currently im doing:

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
Kaboo

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




David
David Ing
reply 5

You can use google to search for other answers

 

More Articles

• How to use ApplyCustomType?
• Is the InfoPath SDK for Visual Studio 2005 Available?
• "RaceOnRCWCleanup was detected"
• VSTO Outlook 2007 Form Region Question
• Word.interop - Saveas usage
• Visual Studio 2005 Tools for Office Hands-on Labs for Word 2003
• Project server error
• Excel Automation
• How to use Office commandbar objects in common VB application?
• Do userproperties exist on a email folder?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Determining where I am in ThisDocument_S
• Good tutorial on VSTO
• Weird installing error...
• Mail item icons
• Excel: show XML data horizontally
• How to put new column into generated Lis
• Action Pane disappears
• Adding outlook appointment item to share
• Issues with Outlook Add-in - recommendat
• Using a template to add toolbar
• Adding controls at runtime resulting error
• 4 Lines C++ Code, How it is working?
• Unable to rebuild my project
• Access Developer Extension
• Build an Action Pane Across All Document

Hot Articles

• Edit box on toolbar
• Dynamic SmartTag-actions
• Problem deploying .Net COM Add-In
• Error when opening VSTO sample project
• How do I submit a BUG for office in gene
• Upgrading excel workbook solution from V
• VSTO and AVG...HEEELP!
• Client Appl capable of Importing a XML f
• Excel IF Function
• Mapping CustomXML data into ContentContr
• Newbie Develop Office App using .Net
• Updating the add-in without RE-install ..
• Persistent menu in Word (through VSTO 20
• WebBrowser Control Hosting Word: How do
• What is the best way to change an Excel

Recommend Articles

• VSTO 2005 questions...
• VSTO 2005 SE Deployment Problem
• how to publish vsto word document in to
• Adding dynamic menus and events in Outlook
• how to import a .vcf file into outlook p
• VSTO 2005 Outlook Add-In Security Access
• VB.net equivalents of VBA Macro code
• How to Configure OfficeVSTOBenefitsRegis
• Get Reference to the Excel Worksheet fro
• VSTO update database
• insertxml in word 2007
• vsto - .Net framework 2 security - publi
• VSTO Installation
• Unable to uninstall
• Manual Page breaks vs automatic page bre