I want to download 4 unique csv files that I receive daily. So I need to download these 4 automatically. As of now, I can download all csv files but I can't limit it to only today's date.
This is my current code.
Public Sub SaveAutoAttach(item As Outlook.MailItem)
Dim object_attachment As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Desktop\Automatic Outlook Downloads"
For Each object_attachment In item.Attachments
If InStr(object_attachment.DisplayName, ".csv") Then
'If Int(object_attachment.ReceivedTime) = Date Then
object_attachment.SaveAsFile saveFolder & "\" & object_attachment.DisplayName
End If
'End If
Next
End Sub
I was able to answer my own question. Below is my modified code.
Public Sub SaveAutoAttach(item As Outlook.MailItem)
Dim olApp As Object
Dim olNS As Object
Dim myDate As Date
Dim olItems As Object
Dim olItem As Object
Dim olAttach As Object
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
Flg = True
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items
For Each olItem In olItems
If olItem.ReceivedTime > Date Then
On Error GoTo Finished
Set olAttach = olItem.Attachments.item(1)
Err.Clear: On Error GoTo 0
If Not olAttach Is Nothing Then
If olAttach.FileName Like "*.csv" Then
On Error GoTo Finished
olAttach.SaveAsFile "C:\Desktop\Automatic Outlook Downloads" & "\" & olAttach.FileName
Set olAttach = Nothing
Set olItem = Nothing
End If
End If
End If
Next
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.