'' This script will download all the email attachments which is having csv files.
Option Private Module
Option Explicit
'''''''''''''''''''''''''''''''' Variable Declaration '''''''''''''''''''''''''''''
Const fPath = "C:\Users\Automation\Files\Script\"
Dim OtlApp, mlItems, aCounter, fCounter, mail, mlRcvdDT, OtlFolder, flName, AttachName
Sub Data_Download()
On Error Resume Next
Set OtlApp = GetObject(, "Outlook.application") 'gives error 429 if outlook is not open
If Err.Number = 429 Then
Err.Clear
Shell "outlook.exe", vbMaximizedFocus ''''''''''''''''''''''' open outlook '''''''''''''
Application.Wait Now + TimeValue("00:00:10") ''''''''''''' wait for 10 seconds until all the emails will be loaded '''''
Set OtlApp = GetObject("", "outlook.application")
End If
'' create outlook object
Set OtlFolder = OtlApp.GetNamespace("Mapi").GetDefaultFolder(6).Parent.Folders.Item("Data_Folder") '''''''''''''''''' update the desired folder name
Set mlItems = OtlFolder.Items
mlItems.Sort "ReceivedTime", True
For Each mail In mlItems
mlRcvdDT = CDate(FormatDateTime(mail.ReceivedTime, vbShortDate))
AttachName = "DataFile_" & Format(Date, "dd-mm-yyyy") & ".csv" '''''''''''''''''''''''''''''''' change .csv to desired file format
If Date = mlRcvdDT Then
aCounter = mail.Attachments.Count
If aCounter > 0 Then
For fCounter = 1 To aCounter
If InStr(1, mail.Attachments(fCounter).Filename, ".csv", vbTextCompare) > 0 Then '''''''''''''''''''''''''''''''' change .csv to desired file format
mail.Attachments(fCounter).SaveAsFile fPath & flName & AttachName
mail.UnRead = True
End If
Next
End If
End If
Next
OtlApp.Quit
Set OtlApp = Nothing
End Sub
Option Private Module
Option Explicit
'''''''''''''''''''''''''''''''' Variable Declaration '''''''''''''''''''''''''''''
Const fPath = "C:\Users\Automation\Files\Script\"
Dim OtlApp, mlItems, aCounter, fCounter, mail, mlRcvdDT, OtlFolder, flName, AttachName
Sub Data_Download()
On Error Resume Next
Set OtlApp = GetObject(, "Outlook.application") 'gives error 429 if outlook is not open
If Err.Number = 429 Then
Err.Clear
Shell "outlook.exe", vbMaximizedFocus ''''''''''''''''''''''' open outlook '''''''''''''
Application.Wait Now + TimeValue("00:00:10") ''''''''''''' wait for 10 seconds until all the emails will be loaded '''''
Set OtlApp = GetObject("", "outlook.application")
End If
'' create outlook object
Set OtlFolder = OtlApp.GetNamespace("Mapi").GetDefaultFolder(6).Parent.Folders.Item("Data_Folder") '''''''''''''''''' update the desired folder name
Set mlItems = OtlFolder.Items
mlItems.Sort "ReceivedTime", True
For Each mail In mlItems
mlRcvdDT = CDate(FormatDateTime(mail.ReceivedTime, vbShortDate))
AttachName = "DataFile_" & Format(Date, "dd-mm-yyyy") & ".csv" '''''''''''''''''''''''''''''''' change .csv to desired file format
If Date = mlRcvdDT Then
aCounter = mail.Attachments.Count
If aCounter > 0 Then
For fCounter = 1 To aCounter
If InStr(1, mail.Attachments(fCounter).Filename, ".csv", vbTextCompare) > 0 Then '''''''''''''''''''''''''''''''' change .csv to desired file format
mail.Attachments(fCounter).SaveAsFile fPath & flName & AttachName
mail.UnRead = True
End If
Next
End If
End If
Next
OtlApp.Quit
Set OtlApp = Nothing
End Sub
No comments:
Post a Comment