title: Error 440 "Array Index out of Bounds"
body: <p>I am trying to download an Excel attachment with the subject keyword.</p>
  
  <p>I managed to create a code but sometimes it is giving <strong>Error 440</strong> <code>"Array Index out of Bounds"</code>.</p>
  
  <p>The code got stuck in this part.</p>
  
  <pre><code>If Items(i).Class = Outlook.OlObjectClass.OlMail Then
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>Sub Attachment()  
      Dim N1 As String
      Dim En As String
      En = CStr(Environ("USERPROFILE"))
      saveFolder = En &amp; "\Desktop\"
      N1 = "Mail Attachment"
  
      If Len(Dir(saveFolder &amp; N1, vbDirectory)) = 0 Then
          MkDir (saveFolder &amp; N1)
      End If
  
      Call Test01
  
  End Sub
  
  Private Sub Test01()
  
      Dim Inbox As Outlook.Folder
      Dim obj As Object
      Dim Items As Outlook.Items
      Dim Attach As Object
      Dim MailItem As Outlook.MailItem
      Dim i As Long
      Dim Filter As String
      Dim saveFolder As String, pathLocation As String
      Dim dateFormat As String
      Dim dateCreated As String
      Dim strNewFolderName As String
      Dim Creation As String
  
      Const Filetype1 As String = "xlsx"
      Const Filetype2 As String = "xlsm"
      Const Filetype3 As String = "xlsb"
      Const Filetype4 As String = "xls"
  
      Dim Env As String
      Env = CStr(Environ("USERPROFILE"))
      saveFolder = Env &amp; "\Desktop\Mentor Training\"
  
      Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
  
      'If Inbox.Items.Restrict("[UnRead] = True").Count = 0 Then
       '   MsgBox "No Mentor Training Mail In Inbox"
       '   Exit Sub
      'End If
  
      Filter = "@SQL=" &amp; Chr(34) &amp; "urn:schemas:httpmail:datereceived" &amp; _
          Chr(34) &amp; " &gt;= '4/2/2017' AND " &amp; _
          Chr(34) &amp; "urn:schemas:httpmail:hasattachment" &amp; _
          Chr(34) &amp; "=1 AND" &amp; Chr(34) &amp; _
          Chr(34) &amp; "urn:schemas:httpmail:read" &amp; _
          Chr(34) &amp; "= 0"
  
      Set Items = Inbox.Items.Restrict(Filter)
  
      For i = 1 To Items.Count
          If Items(i).Class = Outlook.OlObjectClass.olMail Then
              Set obj = Items(i)
              Debug.Print obj.subject
              For Each Attach In obj.Attachments
                  If Right(LCase(Attach.fileName), Len(Filetype1)) = Filetype1 Then 'For searching only excel files
                      dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                      Attach.SaveAsFile saveFolder &amp; "(" &amp; dateFormat &amp; ")" &amp; " " &amp; Attach
                  End If
                  If Right(LCase(Attach.fileName), Len(Filetype2)) = Filetype2 Then 'For searching only excel files
                      dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                      Attach.SaveAsFile saveFolder &amp; "(" &amp; dateFormat &amp; ")" &amp; " " &amp; Attach
                  End If
                  If Right(LCase(Attach.fileName), Len(Filetype3)) = Filetype3 Then 'For searching only excel files
                      dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                      Attach.SaveAsFile saveFolder &amp; "(" &amp; dateFormat &amp; ")" &amp; " " &amp; Attach
                  End If
                  If Right(LCase(Attach.fileName), Len(Filetype4)) = Filetype4 Then 'For searching only excel files
                      dateFormat = Format(obj.ReceivedTime(), "dd-mm-yyyy hh-mm")
                      Attach.SaveAsFile saveFolder &amp; "(" &amp; dateFormat &amp; ")" &amp; " " &amp; Attach
                  End If
                  obj.UnRead = False
                  DoEvents
                  obj.Save
              Next
  
          End If
      Next
      MsgBox "Attachment Saved"
  End Sub
  </code></pre>