title: Script for retrieving emails from Outlook
body: <p>This is my script for retrieving emails from Outlook inbox and store their attachments.</p>
  
  <p>The problem is that is not working properly. It is able to collect the attachments from unread emails and store them into <code>SavePath</code>, but somehow it is stopping when it has read three emails.</p>
  
  <p>The strange thing is that it is able to collect the remaining emails when it is executed again, but not all remaining unread emails.</p>
  
  <p><strong>E.g:</strong> I have 6 unread emails at Inbox, the first time it is executed only 3 emails were read and their attachments were collected. The second time it is executed, it reads the next 2 emails. The third time it is executed, the remaining email was read.</p>
  
  <p>I can't figure out what is happening and why not all emails are read at once.</p>
  
  <pre><code>Dim SavePath
  Dim Subject
  Dim FileExtension
  
  SavePath = "C:\IN\"
  Subject = "'Transfer File'"
  FileExtension = "ARmessage"
  
  Set objOutlook = CreateObject("Outlook.Application")
  Set objNamespace = objOutlook.GetNamespace("MAPI")
  Set objFolder = objNamespace.GetDefaultFolder(6) 'Inbox
  
  Set colItems = objFolder.Items
  Set colFilteredItems = colItems.Restrict("[Unread]=true")
  Set colFilteredItems = colFilteredItems.Restrict("[Subject] = " &amp; Subject)
  
  For Each objMessage In colFilteredItems
      intCount = objMessage.Attachments.Count
      If intCount &gt; 0 Then
          For i = 1 To intCount
              ' if right(Ucase(objMessage.Attachments.Item(i)),9) = FileExtension then
                  objMessage.Attachments.Item(i).SaveAsFile SavePath &amp;  _
                      objMessage.Attachments.Item(i).FileName
              ' End If
          Next
          objMessage.Unread = False
      End If
  Next
  </code></pre>