saving specific attachments from Outlook to a folder

Rasberry

Board Regular
Joined
Aug 11, 2002
Messages
201
Office Version
  1. 365
I want to save attachments in my Inbox from a specific sender to a folder I:\Securities\Cash Commitments & Projection\attachment name_date.xls. I have this code in VBA outlook that I lifted from an example, but I can't figure out what to change to make it sender-specific.


Public Sub Application_Startup()

Dim MItem As MailItem
Dim oAttachment As Attachment
Dim sSaveFolder As String
Dim oDefInbox As Folder
Dim targetFolder As Folder
Dim myItems As Outlook.Items
Dim Item As Object

Set oDefInbox = Session.GetDefaultFolder(olFolderInbox)
Set targetFolder = Session.GetDefaultFolder(olFolderInbox)

sSaveFolder = "I:\Securities\Cash Commitments & Projection"
For Each MItem In targetFolder.Items
If MItem.UnRead = True Then
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Set oAttachment = Nothing
Next oAttachment
MItem.UnRead = False
End If
Next
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I want to save attachments in my Inbox from a specific sender to a folder I:\Securities\Cash Commitments & Projection\attachment name_date.xls.

Just .xls attachments then? Try this, though not tested within Outlook. The key is getting a collection of Input items using the Restrict method and specifying either the sender's name or their email address.

VBA Code:
Public Sub Application_Startup()

    Dim MItem As MailItem
    Dim oAttachment As Attachment
    Dim sSaveFolder As String
    Dim oDefInbox As Folder
    Dim targetFolder As Folder
    Dim myItems As Outlook.Items
    Dim Item As Object
    
    Set oDefInbox = Session.GetDefaultFolder(olFolderInbox)
    Set targetFolder = Session.GetDefaultFolder(olFolderInbox)
    
    sSaveFolder = "I:\Securities\Cash Commitments & Projection\"   'Note - ends with \
    
    Dim sender As String
    sender = "Joe Bloggs"
    'sender = "Joe@bloggs.com"
    
    Set myItems = targetFolder.Items.Restrict("[SenderName] = '" & sender & "'")
    'Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "'")
    For Each Item In myItems
        If Item.Class = OlObjectClass.olMail Then
            For Each oAttachment In Item.Attachments
                With oAttachment
                    If LCase(Right(.Filename, 4)) = ".xls" Then
                        .SaveAsFile sSaveFolder & Left(.Filename, InStrRev(.Filename, ".") - 1) & Format(Date, "_yyyymmdd") & Mid(.Filename, InStrRev(.Filename, "."))
                    End If
                End With
            Next oAttachment
        End If
    Next
    
End Sub
 
Upvote 0
Solution
Just .xls attachments then? Try this, though not tested within Outlook. The key is getting a collection of Input items using the Restrict method and specifying either the sender's name or their email address.

VBA Code:
Public Sub Application_Startup()

    Dim MItem As MailItem
    Dim oAttachment As Attachment
    Dim sSaveFolder As String
    Dim oDefInbox As Folder
    Dim targetFolder As Folder
    Dim myItems As Outlook.Items
    Dim Item As Object
   
    Set oDefInbox = Session.GetDefaultFolder(olFolderInbox)
    Set targetFolder = Session.GetDefaultFolder(olFolderInbox)
   
    sSaveFolder = "I:\Securities\Cash Commitments & Projection\"   'Note - ends with \
   
    Dim sender As String
    sender = "Joe Bloggs"
    'sender = "Joe@bloggs.com"
   
    Set myItems = targetFolder.Items.Restrict("[SenderName] = '" & sender & "'")
    'Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "'")
    For Each Item In myItems
        If Item.Class = OlObjectClass.olMail Then
            For Each oAttachment In Item.Attachments
                With oAttachment
                    If LCase(Right(.Filename, 4)) = ".xls" Then
                        .SaveAsFile sSaveFolder & Left(.Filename, InStrRev(.Filename, ".") - 1) & Format(Date, "_yyyymmdd") & Mid(.Filename, InStrRev(.Filename, "."))
                    End If
                End With
            Next oAttachment
        End If
    Next
   
End Sub
This is the line that won't execute:
Set myItems = targetFolder.Items.Restrict("Passport_Reporting@NTRS.com = '" & sender & "'")
'Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "'")

I have tried it with the brackets and without the brackets. The error message says:
1689340231873.png
 
Upvote 0
Don't change the Restrict line - [SenderName] and [SenderEmailAddress] are named 'conditions' in Outlook.

Since you've specified the user's email address, and not their name, you want to specify the [SenderEmailAddress] condition with their email address. Therefore:

VBA Code:
    'sender = "Joe Bloggs"
    sender = "Passport_Reporting@NTRS.com"
 
    'Set myItems = targetFolder.Items.Restrict("[SenderName] = '" & sender & "'")
    Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "'")
 
Upvote 0
Don't change the Restrict line - [SenderName] and [SenderEmailAddress] are named 'conditions' in Outlook.

Since you've specified the user's email address, and not their name, you want to specify the [SenderEmailAddress] condition with their email address. Therefore:

VBA Code:
    'sender = "Joe Bloggs"
    sender = "Passport_Reporting@NTRS.com"
 
    'Set myItems = targetFolder.Items.Restrict("[SenderName] = '" & sender & "'")
    Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "'")
Thank you so much--that doesn't error out, now!! It also saves the file where I want it with today's date. There is only one problem -- it saves each attachment over each other so there is only one file saved. Ideally, I would like each attachment to have the date it was last modified or date of the email. Or, if it is easier to code, simply look at today or yesterday's emails and place that attachment in the file.
 
Upvote 0
Ideally, I would like each attachment to have the date it was last modified or date of the email.

This saves the attachments with the date of the email:
VBA Code:
                        .SaveAsFile sSaveFolder & Left(.Filename, InStrRev(.Filename, ".") - 1) & Format(Item.ReceivedTime, "_yyyymmdd") & Mid(.Filename, InStrRev(.Filename, "."))
 
Upvote 0
This saves the attachments with the date of the email:
VBA Code:
                        .SaveAsFile sSaveFolder & Left(.Filename, InStrRev(.Filename, ".") - 1) & Format(Item.ReceivedTime, "_yyyymmdd") & Mid(.Filename, InStrRev(.Filename, "."))
Thank works! Outstanding. Thank you!
 
Upvote 0
Thank works! Outstanding. Thank you!

The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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