Bad Outlook Programming Model: No Outlook.Item exposed!

Item Enumeration

Outlook (probably for backward compatibility reasons) doesn’t have a default Outlook.Item object as the parent of the various Outlook.Items (Outlook.MailItem, PostItem, AppointmentItem). This means code like this wouldn’t work:

dim oItem as Outlook.MailItem
for each oItem in Outlook.ActiveExplorer.Selection
' you get errors for any items (like PostItem), that isn't an 
MailItem in your inbox
MsgBox oItem.Subject
next

Figure: Example of incorrect code

So you have to write something like this:

dim oItem as Object
for each oItem in Outlook.ActiveExplorer.Selection
' this will work, because every type of Outlook Item has the subject property... 
' but we're relying on reflection (late binding) here and 
' intellisense doesn't help us when we write code,
' compiler doesn't tell us if we made a typo,
' late binding is slower...
MsgBox oItem.Subject
next

Figure: Example of correct code