title: Looping through Outlook mail items
body: <p>I am scratching my head on this one, I am fairly new to VBA (and programming in general) and would like this code improved. Any ideas on how to cover all mail items in main folders, sub folders, sub sub folders with an improved or simplified code?</p>
  
  <p>1 level down:</p>
  
  <ul>
  <li>Inbox</li>
  <li>Deleted</li>
  </ul>
  
  <p>2 levels down:</p>
  
  <ul>
  <li>Inbox -> Pending</li>
  <li>Inbox -> user folder</li>
  </ul>
  
  <p>3 levels down:</p>
  
  <ul>
  <li>Inbox -> Pending -> Important</li>
  <li>Inbox -> user folder -> user sub folder</li>
  </ul>
  
  <p><strong>My code so far is:</strong></p>
  
  <pre><code>Sub GetEmailsDetailsMINE()
  
  Dim outlook_app As Outlook.Application
  Dim namespace As Outlook.namespace
  
  Set outlook_app = New Outlook.Application
  Set namespace = outlook_app.GetNamespace("MAPI")
  
  Dim account_folder As Outlook.MAPIFolder
  Dim main_folder As Outlook.MAPIFolder
  Dim sub_folder1 As Outlook.MAPIFolder
  Dim sub_folder2 As Outlook.MAPIFolder
  
  On Error Resume Next
  
  Dim obj_mail As Outlook.MailItem
  Dim rowNumber As Integer
  rowNumber = 2
  
  For Each account_folder In namespace.Folders
      ' main account, eg someone@company.com
      For Each main_folder In account_folder.Folders
          ' 1 level down, find emails here
          For Each obj_item In main_folder.Items
              If obj_item.Class = olMail Then
                  Set obj_mail = obj_item
                  Cells(rowNumber, 1) = obj_mail.SenderEmailAddress
                  Cells(rowNumber, 2) = obj_mail.To
                  Cells(rowNumber, 3) = obj_mail.Subject
                  Cells(rowNumber, 4) = obj_mail.ReceivedTime
                  Cells(rowNumber, 5) = obj_mail.EntryID
                  Cells(rowNumber, 6) = main_folder.Name
                  rowNumber = rowNumber + 1
              End If
          Next obj_item
          For Each sub_folder1 In main_folder.Folders
              ' two levels down, find emails here
              For Each obj_item In sub_folder1.Items
                          If obj_item.Class = olMail Then
                              Set obj_mail = obj_item
                              Cells(rowNumber, 1) = obj_mail.SenderEmailAddress
                              Cells(rowNumber, 2) = obj_mail.To
                              Cells(rowNumber, 3) = obj_mail.Subject
                              Cells(rowNumber, 4) = obj_mail.ReceivedTime
                              Cells(rowNumber, 5) = obj_mail.EntryID
                              Cells(rowNumber, 6) = sub_folder1.Name
                              rowNumber = rowNumber + 1
                          End If
              Next obj_item
  
              ' three levels down
              For Each sub_folder2 In sub_folder1.Folders
                      For Each obj_item In sub_folder2.Items
                          If obj_item.Class = olMail Then
                              Set obj_mail = obj_item
                              Cells(rowNumber, 1) = obj_mail.SenderEmailAddress
                              Cells(rowNumber, 2) = obj_mail.To
                              Cells(rowNumber, 3) = obj_mail.Subject
                              Cells(rowNumber, 4) = obj_mail.ReceivedTime
                              Cells(rowNumber, 5) = obj_mail.EntryID
                              Cells(rowNumber, 6) = sub_folder1.Name &amp; " || " &amp; sub_folder2.Name
                              rowNumber = rowNumber + 1
                          End If
                      Next obj_item
              Next sub_folder2
  
          Next sub_folder1
      Next main_folder
  Next account_folder
  
  On Error GoTo 0
  
  End Sub
  </code></pre>
  
  <p>This works fine, I can get all the items I want but somehow I find it repetitive. Any ideas on how to improve my code?</p>