(Solved) – Extract email attachments from date received – VBA


thanks in advance for help!

Ihave one code which help me to extract all email attachments from specific email folder. it works perfectly. But now I want to change to extract email attachments starting from date which I enter in dialog box (I want to extract email attachments just for emails which I received in last seven day not the whole folder). Please find below code for your reference:

Sub Extract_emails()
Dim OlApp As Object
Dim OlMail As Object
Dim OlItems As Object
Dim Olfolder As Object
Dim J As Integer
Dim strFolder As String

Set OlApp = GetObject(, "Outlook.Application")        
If Err.Number = 429 Then
Set OlApp = CreateObject("Outlook.Application")   
End If

strFolder = ThisWorkbook.Path & "Extract"            
Set Olfolder = OlApp.getnamespace("MAPI").Folders("MyEmailAddress").Folders("Inbox")
Set OlItems = Olfolder.Items

For Each OlMail In OlItems
If OlMail.Attachments.Count > 0 Then
    For J = 1 To OlMail.Attachments.Count
    OlMail.Attachments.Item(J).SaveAsFile strFolder & "" & OlMail.Attachments.Item(J).Filename
    Next J

End If

Set OlApp = Nothing
Set OlMail = Nothing
Set OlItems = Nothing
Set Olfolder = Nothing


MsgBox ("Done")

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *