title: type mismatch error on loop in vba
body: <p>I'm working in Outlook VBA and have constructed a For Next loop to read in the body of MailItems which are formatted like Key=Value pairs.  To a point it seems to be working, but on the end of the second iteration when it reaches the "Next oitem" I get the error thrown "type mismatch".  Well,there is still a third MailItem to be read in, so I don't know why I am getting this error.  Any guidance would be appreciated.</p>
  
  <pre><code>Sub ReadMailItems()
  
  Dim olapp As Outlook.Application
  Dim olappns As Outlook.NameSpace
  Dim oitem As Outlook.MailItem
  Dim ItemsToProcess As Outlook.Items
  Dim myFolder As MAPIFolder
  Dim sFilter As String
  Dim dailyStats As CRBHA_Stats
  Dim kvPairs As Collection
  Dim Item As KeyValuePair
  Dim today As Date
  today = Date
  
  On Error GoTo LocalErr
  
  'set outlook objects
  Set olapp = New Outlook.Application
  Set olappns = olapp.GetNamespace("MAPI")
  Set myFolder = olappns.GetDefaultFolder(olFolderInbox)
  'Filter or only MailItems received today
  sFilter = "[ReceivedTime] &gt;= " &amp; AddQuotes(Format(Date, "ddddd"))
  Set ItemsToProcess = Session.GetDefaultFolder(olFolderInbox).Items.Restrict(sFilter)
  Set StatsCollection = New Collection
  
  For Each oitem In ItemsToProcess
   If CheckSubject(oitem.Subject) Then
     Set kvPairs = GetKeyValuePairs(oitem.body)
     'Iterate over the Collection and load up
     'an instance of CRBHA_Stats object
     Set dailyStats = New CRBHA_Stats
     dailyStats.SubmissionDate = today
     For Each Item In kvPairs
       If LCase(Item.Key) = LCase("EmployeeID") Then
          dailyStats.EmployeeID = Item.Value
       ElseIf LCase(Item.Key) = LCase("Approved") Then
          dailyStats.Approved = Item.Value
       ElseIf LCase(Item.Key) = LCase("Declined") Then
          dailyStats.Declined = Item.Value
       ElseIf LCase(Item.Key) = LCase("PFA") Then
          dailyStats.PFAs = Item.Value
       ElseIf LCase(Item.Key) = LCase("Followups") Then
          dailyStats.FollowUps = Item.Value
       ElseIf LCase(Item.Key) = LCase("CRA") Then
          dailyStats.CRAs = Item.Value
       End If
      Next Item
  
      'Add each CRBHA_Stats object to the StatsCollection
      StatsCollection.Add dailyStats
  
      Debug.Print dailyStats.ToString
      Debug.Print "_____________" &amp; vbCrLf
    End If
  Next oitem   '&lt;&lt;&lt;&lt;This is where it cuts out
  
  ExitProc:
  Set olapp = Nothing
  Set olappns = Nothing
  Set myFolder = Nothing
  Set ItemsToProcess = Nothing
  Set dailyStats = Nothing
  Exit Sub
  
  LocalErr:
    If Err.Number &lt;&gt; 0 Then
     Msg = "Error # " &amp; Str(Err.Number) &amp; " was generated by " _
         &amp; Err.Source &amp; Chr(13) &amp; "Error Line: " &amp; Erl &amp; Chr(13) &amp; Err.Description
     MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
     End If
     'Resume Next
  
  
  End Sub
  </code></pre>