saving email attachments based on sender AND timestamp.

Rasberry

Board Regular
Joined
Aug 11, 2002
Messages
201
Office Version
  1. 365
The following code works for saving all attachments from the sender. However, I want to run the code daily and save just yesterday's and today's attachments from the sender:

Public Sub Application_SBA()

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
Dim Filter As String

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

sSaveFolder = "I:\Securities\Asset Classes in the Portfolio\Bond Holdings\SBA RMOF\A - Daily email files\" 'Note - ends with \



Dim sender As String
sender = "fta@sba.gov"
'sender = "fta@sba.gov"

Filter = "@SQL=" & "%today(" & Chr(34) & ("urn:schemas:httpmail:datereceived") & _
Chr(34) & ")%"

Set myItems = targetFolder.Items.Restrict(Filter)
Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & 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
.SaveAsFile sSaveFolder & oAttachment.DisplayName
End With
Next oAttachment
End If
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Set myItems = targetFolder.Items.Restrict("[SenderEmailAddress] = '" & sender & "' And [ReceivedTime]>=""&Date()12:00am&""")

The above line works to limit the saving of email attachments based on sender and what was sent today (after midnight).
 
Upvote 0
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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