title: VBA in MS Outlook to filter by date and subject, extract attachment, save and replace previous file in folder
body: <p>I get a generated report by Oracle web app every week. I got a macro working to extract that attachment report from my email, but for some reason the date filter doesn't do anything and it saves all the attachments with the email subject "VERIPRD: XXVER Veritiv Aging Report Main: PETROP01" (which is the subject of the report that I want, but I get this weekly, and I only need to extract the most current one)</p>
  
  <p>Also, the report comes with a .out extension which can be opened up with Excel, but if I save that file within the macros as xlsx it gets corrupted.</p>
  
  <p>So what I need is for this macro to actually filter by date, and Subject line (mentioned above), save the .out file as an Excel file titled "Aging Report" and, if there's already an "Aging Report" in destination folder, to replace that previous excel file and not prompt with a message asking me if I want to replace it.</p>
  
  <p>Here's the code I have so far which I put in MS outlook:</p>
  
  <pre><code>Public Sub Extract_Outlook_Email_Attachments()
   Dim OutlookOpened As Boolean
   Dim outApp As Outlook.Application
   Dim outNs As Outlook.NameSpace
   Dim outFolder As Outlook.MAPIFolder
   Dim outAttachment As Outlook.attachment
   Dim outItem As Object
   Dim saveFolder As String
   saveFolder = "C:\Users\borjax01\Desktop\aging reports"
   Dim outMailItem As Outlook.MailItem
   Dim inputDate As String, subjectFilter As String
   If Right(saveFolder, 1) &lt;&gt; "\" Then saveFolder = saveFolder &amp; "\" &amp; "Aging 
   Report.out"
  
   inputDate = InputBox("Enter date to filter the email subject", "Extract 
   Outlook email attachments")
   If inputDate = "" Then Exit Sub
  
   InputDateFilter = inputDate
   subjectFilter = ("VERIPRD : XXVER Veritiv Aging Report Main : PETROP01")
  
  
   OutlookOpened = False
   On Error Resume Next
   Set outApp = GetObject(, "Outlook.Application")
   If Err.Number &lt;&gt; 0 Then
       Set outApp = New Outlook.Application
       OutlookOpened = True
   End If
   On Error GoTo 0
  
   If outApp Is Nothing Then
       MsgBox "Cannot start Outlook.", vbExclamation
       Exit Sub
   End If
  
   Set outNs = outApp.GetNamespace("MAPI")
   Set outFolder = outNs.PickFolder
  
  
   If Not outFolder Is Nothing Then
      For Each outItem In outFolder.Items
          If outItem.Class = Outlook.OlObjectClass.olMail Then
              Set outMailItem = outItem
               If outMailItem.Subject = subjectFilter Then
                   Debug.Print outMailItem.Subject
                   For Each outAttachment In outMailItem.Attachments
                  outAttachment.SaveAsFile saveFolder
  
  
                Set outAttachment = Nothing
  
  
                   Next
               End If
           End If
       Next
   End If
  
   If OutlookOpened Then outApp.Quit
  
   Set outApp = Nothing
  
  End Sub
  </code></pre>