title: Looping over Outlook mail items in "Sent Items" folder
body: <p>We're trying to access the <strong>Sent Items</strong> folder in <strong>Outlook 2007 (using Exchange)</strong> but the test for <code>TypeOf(i) Is Outlook.MailItem</code> in the below code snippet always returns False.</p>
  
  <p>We know we have the right folder because a test for <code>items.Count</code> returns the correct number of mail items.</p>
  
  <p>Inbox messages are fine. If we change the folder from <code>olFolderSentMail</code> to <code>olFolderInbox</code> the test for <code>TypeOf(i) Is Outlook.MailItem</code> passes and it's quite happy to show us the Subject.</p>
  
  <pre><code>Dim app As Outlook.Application = Nothing
  Dim ns As Outlook.NameSpace = Nothing
  Dim siFolder As Outlook.Folder = Nothing
  Dim items As Outlook.Items = Nothing
  
  app = New Outlook.Application()
  ns = app.Session
  
  
  siFolder = CType(ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail), Outlook.Folder)
  
  items = siFolder.Items
  
  MsgBox(items.Count)
  
  For Each i In items
  
      If TypeOf (i) Is Outlook.MailItem Then
          Dim mailitem As Outlook.MailItem
          mailitem = CType(i, Outlook.MailItem)
          MsgBox(mailitem.Subject)
      Else
          MsgBox("not a mailitem")
      End If
  Next
  </code></pre>
  
  <h2>Update</h2>
  
  <p>@Rob's answer below, yes, definitely has helped. But I'm still puzzled. The crucial thing @Rob's code is doing is testing for <code>.MessageClass = "IPM.Note"</code>. If I include that then the later test for <code>TypeOf x Is MailItem</code> succeeds. If I replace @Rob's test for <code>.MessageClass = "IPM.Note"</code> with <code>If True Then</code> then the same code still executes but the later test for Is MailItem fails. It's as if just testing for the <code>.MessageClass</code> automagically resolves the object into a MailItem.</p>
  
  <p>Furthermore the Sent Items don't contain any meeting requests so the test would seem to be unnecessary anyway.</p>