title: Excel VBA Code to retrieve e-mails from outlook
body: <p>I am to write a VBA code that would retrive emails from Outlook based on certain conditions. The problem I have is that I have to denote a certain folder in my code (in the example below the folder denoted is "PRE Costumer". I would like to retrive all emails from my 'inbox' or in better case from all outlook folders. The problem is that my inbox consists of many subfolders (because of rules0. My problem is that I may not know all the subfolders names (as many useres are going to use the macro and even someone can have the e mails in Personal Folders).<br>
  Could you please advise is there a way to overcome this problem?<br>
  Please let me know if this question is vague (as I am newcomer)  </p>
  
  <p>Please find the line that I have probelm with marked with a comment.</p>
  
  <pre><code>Sub GetFromInbox()
  
  Dim olApp As Outlook.Application
  Dim olNs As Namespace
  Dim Fldr As MAPIFolder
  Dim olMail As Variant
  Dim i As Integer
  
  Set olApp = New Outlook.Application
  Set olNs = olApp.GetNamespace("MAPI")
  'Below is the line I have problem with
  Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer") 
  
  i = 1
  x = Date
  
  For Each olMail In Fldr.Items
      If InStr(olMail.Subject, "transactions") &gt; 0 _
      And InStr(olMail.ReceivedTime, x) &gt; 0 Then  
          ActiveSheet.Cells(i, 1).Value = olMail.Subject
          ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
          ActiveSheet.Cells(i, 3).Value = olMail.SenderName
          i = i + 1
      End If
  Next olMail
  
  Set Fldr = Nothing
  Set olNs = Nothing
  Set olApp = Nothing
  End Sub
  </code></pre>