Download Attachments

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have the code which will down load attachments.
But need to modify the code so that it will only download on a specify subject line attachments even if the mail is read or unread.
Subject is "Open & Closed 2020-11-05" it also has the date. so every time date will change

Don't want to create any rules on the outlook
Any Idea

VBA Code:
Sub DownloadAttachmentFirstUnreadEmail()
    Dim oOlAp As Object, oOlns As Object, oOlInb As Object
    Dim oOlItm As Object, oOlAtch As Object
    '~~> New File Name for the attachment
    Dim NewFileName As String
    NewFileName = AttachmentPath   '& Format(Date, "DD-MM-YYYY") & "-"
    '~~> Get Outlook instance
    Set oOlAp = GetObject(, "Outlook.application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    '~~> Check if there are any actual unread emails
    If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
        MsgBox "NO Unread Email In Inbox"
        Exit Sub
    End If
    '~~> Extract the attachment from the 1st unread email
    For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
        '~~> Check if the email actually has an attachment
        If oOlItm.Attachments.Count <> 0 Then
            For Each oOlAtch In oOlItm.Attachments
                '~~> Download the attachment
                oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
                Exit For
            Next
        Else
            MsgBox "The First item doesn't have an attachment"
        End If
        Exit For
    Next
 End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Change the Restrict filter string (for the current date) to:

VBA Code:
.Restrict("[Subject] = ""Open & Closed " & Format(Date, "YYYY-MM-DD") & """")
 
Upvote 0
"[Subject] = ""Open & Closed " & Format(Date, "YYYY-MM-DD") & """"
did but it going to msgbox
VBA Code:
oOlInb.Items.Restrict("[Subject] = ""Cisco ENCOs KPIs - Open & Closed" & Format(Date, "YYYY-MM-DD") & """").Count = 0 Then
 
Upvote 0
Which is the correct subject start string? "Open & Closed" or "Cisco ENCOs KPIs - Open & Closed"?

Also, you've missed a space between "Closed" and the date in the latter. Try this:

VBA Code:
oOlInb.Items.Restrict("[Subject] = ""Cisco ENCOs KPIs - Open & Closed " & Format(Date, "YYYY-MM-DD") & """").Count = 0 Then
 
Upvote 0
Which is the correct subject start string? "Open & Closed" or "Cisco ENCOs KPIs - Open & Closed"?

Also, you've missed a space between "Closed" and the date in the latter. Try this:

VBA Code:
oOlInb.Items.Restrict("[Subject] = ""Cisco ENCOs KPIs - Open & Closed " & Format(Date, "YYYY-MM-DD") & """").Count = 0 Then

Still its showing the msgbox "NO Unread Email In Inbox"

VBA Code:
Sub DownloadAttachmentFirstUnreadEmail()
    Dim oOlAp As Object, oOlns As Object, oOlInb As Object
    Dim oOlItm As Object, oOlAtch As Object
    '~~> New File Name for the attachment
    Dim NewFileName As String
    NewFileName = ActiveWorkbook.Path & Format(Date, "DD-MM-YYYY") & "-"
    '~~> Get Outlook instance
    Set oOlAp = GetObject(, "Outlook.application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
    Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    '~~> Check if there are any actual unread emails
    If oOlInb.Items.Restrict("[Subject] = ""Cisco ENCOs KPIs - Open & Closed " & Format(Date, "YYYY-MM-DD") & """").Count = 0 Then
        MsgBox "NO Unread Email In Inbox"
        Exit Sub
    End If
    '~~> Extract the attachment from the 1st unread email
    For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
        '~~> Check if the email actually has an attachment
        If oOlItm.Attachments.Count <> 0 Then
            For Each oOlAtch In oOlItm.Attachments
                '~~> Download the attachment
                oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
                Exit For
            Next
        Else
            MsgBox "The First item doesn't have an attachment"
        End If
        Exit For
    Next
 End Sub
 
Upvote 0
The code works for me. Make sure you have at least 1 email in the Inbox with the exact subject "Cisco ENCOs KPIs - Open & Closed 2020-11-11", without the quotes. Check for extra spaces - leading, trailing, etc.

You'll want to change the MsgBox prompt since the If statement is no longer checking for unread emails.

Also, I suspect the NewFileName = line should be:
VBA Code:
NewFileName = ActiveWorkbook.Path & "\" & Format(Date, "DD-MM-YYYY") & "-"
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,643
Members
452,663
Latest member
MEMEH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top