title: OutLook VBA Email or Notification Causes Out of Bounds Error
body: <p>I have some outlook VBA code which works fine to save attachments, however every time I get an email or a meeting notification in Outlook it causes an instant <em>Out of Bounds</em> error If I don't get any emails or notifications the code will run fine through to completion.</p>
  <p>Is there a way to ensure that these notifications will not stop the code from running?</p>
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  Sub SaveAttachmentsFromSelectedItemsPDF2_ForNext()
      Dim currentItem As Object
      Dim currentAttachment As Attachment
      Dim saveToFolder As String
      Dim savedFileCountPDF As Long
      Dim i As Long
      Dim j As Long
  
      saveToFolder = &quot;c:\dev\outlookexport&quot; 'change the path accordingly
      savedFileCountPDF = 0
  
      For i = 1 To ActiveExplorer.Selection.Count
          Set currentItem = ActiveExplorer.Selection(i)
          For j = 1 To currentItem.Attachments.Count
              Set currentAttachment = currentItem.Attachments(j)
              If UCase(Right(currentAttachment.DisplayName, 5)) = UCase(&quot;.xlsx&quot;) Then
                  currentAttachment.SaveAsFile saveToFolder &amp; &quot;\&quot; &amp; _
                  Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 5) &amp; &quot;.xlsx&quot;
                  savedFileCountPDF = savedFileCountPDF + 1
              End If
              ' If For Next does not release memory automatically then
              ' uncomment to see if this has an impact
              'Set currentAttachment = Nothing
          Next
          ' If For Next does not release memory automatically then
          ' uncomment to see if this has an impact
          'Set currentItem = Nothing
      Next
      MsgBox &quot;Number of PDF files saved: &quot; &amp; savedFileCountPDF, vbInformation
  End Sub
  </code></pre>
  <p>This is what I tried to create from the answer below:</p>
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  Sub SaveAttachmentsFromSelectedItemsPDF2_ForNext()
      Dim currentItem As Object
      Dim currentAttachment As Attachment
      Dim saveToFolder As String
      Dim savedFileCountPDF As Long
      Dim i       As Long
      Dim j       As Long
      Dim x       As Long
      Dim myOlExp As Object
      Dim myOlSel As Object
  
      ' New
      Set myOlExp = Application.ActiveExplorer
      Set myOlSel = myOlExp.Selection
  
      saveToFolder = &quot;c:\dev\outlookexport&quot;        'change the path accordingly
      savedFileCountPDF = 0
  
      For x = 1 To myOlSel.Count
          If myOlSel.Item(x).Class = OlObjectClass.olMail Then
                  Set currentItem = ActiveExplorer.Selection(i)
  
                  For j = 1 To currentItem.Attachments.Count
  
                      Set currentAttachment = currentItem.Attachments(j)
  
                      If UCase(Right(currentAttachment.DisplayName, 5)) = UCase(&quot;.xlsx&quot;) Then
                          currentAttachment.SaveAsFile saveToFolder &amp; &quot;\&quot; &amp; _
                          Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 5) &amp; &quot;.xlsx&quot;
                          savedFileCountPDF = savedFileCountPDF + 1
                      End If
                  Next
              End If
          Next
          MsgBox &quot;Number of PDF files saved: &quot; &amp; savedFileCountPDF, vbInformation
  End Sub
  </code></pre>