title: Count Outlook emails/attachments older than 30 days using Excel VBA
body: <p>I have mailboxes in Outlook, where I need to count the emails older than 30 days.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>Sub HowManyEmails()
  
  Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
  Dim EmailCount As Integer
  
  Set objOutlook = CreateObject("Outlook.Application")
  Set objnSpace = objOutlook.GetNamespace("MAPI")
  
  On Error Resume Next
  
  Set objFolder1 = objnSpace.Folders("Outlook Data File").Folders("Inbox")
  If Err.Number &lt;&gt; 0 Then
      Err.Clear
      MsgBox "No such folder."
      Exit Sub
  End If
  
  For Each MailItem In objFolder1.Items
      If MailItem.ReceivedTime &lt; (Date - 30) Then EmailCount = EmailCount + 1
  Next
  
  Sheets("Sheet1").Range("C2").Value = EmailCount
  
  Set objOutlook = Nothing
  Set objnSpace = Nothing
  Set objFolder = Nothing
  End Sub
  </code></pre>
  
  <p><code>MailItem.ReceivedTime &lt; (Date - 30)</code> does not work as it should. I think it's because the ReceivedTime in Outlook contains the hour/minute.</p>