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
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