title: Counting Outlook outbox items and logging to a file
body: <p>I need a macro that would count the number of sent emails (all in one sent folder, no subfolders) and log the result to a file (csv or txt). The output should file should state number of emails per date, sender address/name and recipient's domain (@company.com). </p>
  
  <p>I was able to get this code to work partially, but it only shows date and number of emails in the output file. </p>
  
  <p>Also - is there a way to append new data to the file, not to overwrite it?</p>
  
  <p>Message Box in Outlook is really optional, output file is the critical part.</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 objFolder = objnSpace.Folders("xxx@company.com").Folders("Outbox")
      If Err.Number &lt;&gt; 0 Then
          Err.Clear
          MsgBox "No such folder."
          Exit Sub
      End If
  
      EmailCount = objFolder.Items.Count
  
      MsgBox "Number of emails in the folder: " &amp; EmailCount, , "email count"
  
      Dim dateStr As String
      Dim myItems As Outlook.Items
      Dim dict As Object
      Dim msg As String
      Set dict = CreateObject("Scripting.Dictionary")
      Set myItems = objFolder.Items
      myItems.SetColumns ("ReceivedTime")
      ' Determine date of each message:
      For Each myItem In myItems
          dateStr = GetDate(myItem.ReceivedTime)
          If Not dict.Exists(dateStr) Then
              dict(dateStr) = 0
          End If
          dict(dateStr) = CLng(dict(dateStr)) + 1
      Next myItem
  
      ' Output counts per day:
      msg = ""
      For Each o In dict.Keys
          msg = msg &amp; o &amp; ": " &amp; dict(o) &amp; " items" &amp; vbCrLf
      Next
  
      Dim fso As Object
      Dim fo As Object
  
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set fo = fso.CreateTextFile("C:\Users\xxx\Documents\outlook_test_log.txt")
      fo.Write msg
      fo.Close
  
      Set fo = Nothing
      Set fso = Nothing
      Set objFolder = Nothing
      Set objnSpace = Nothing
      Set objOutlook = Nothing
  End Sub
  
  Function GetDate(dt As Date) As String
      GetDate = Year(dt) &amp; "-" &amp; Month(dt) &amp; "-" &amp; Day(dt)
  End Function
  </code></pre>