Downloading Attachment from outlook by using VBA in excel based on time of sent

%3CLINGO-SUB%20id%3D%22lingo-sub-2659995%22%20slang%3D%22en-US%22%3EDownloading%20Attachment%20from%20outlook%20by%20using%20VBA%20in%20excel%20based%20on%20time%20of%20sent%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2659995%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20below%20vba%20code%20for%20%22%3CEM%3E%3CSTRONG%3Edownloading%20mail%20attachment%20based%20on%20sender%20name%22.%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3ESome%20time%20the%20sender%20will%20send%20multiple%20files%20in%20a%20day%20itslelf%2C%20but%20I%20want%20to%20download%26nbsp%3B%3CEM%3E%3CSTRONG%3Efirst%3C%2FSTRONG%3E%3C%2FEM%3E%3CEM%3E%26nbsp%3B%3C%2FEM%3Efile%20he%20sent%20to%20me%20using%20the%20vba.%20(Based%20on%20time%20of%20Sending)%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20changes%20to%20be%20done%20in%20the%20code%20in%20this%20case.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3ESub%20DATA()%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20ol%20As%20Object%20'Outlook.Application%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20ns%20As%20Object%20'Outlook.Namespace%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20fol%20As%20Object%20'Outlook.Folder%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20i%20As%20Object%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20mi%20As%20Object%20'Outlook.MailItem%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20at%20As%20Object%20'Outlook.Attachment%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20fso%20As%20Object%20'Scripting.FileSystemObject%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20dir%20As%20Object%20'Scripting.Folder%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20dirName%20As%20String%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20oFSO%20As%20Object%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20oFolder%20As%20Object%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20oFile%20As%20Object%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20f%20As%20Integer%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3E'%20change%201%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20inboxFol%20As%20Object%20'Outlook.Folder%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EDim%20subFol%20As%20Object%20'Outlook.Folder%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3E'Some%20Set%20Ups%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20fso%20%3D%20CreateObject(Class%3A%3D%22Scripting.FileSystemObject%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20ol%20%3D%20CreateObject(Class%3A%3D%22Outlook.Application%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20ns%20%3D%20ol.GetNamespace(%22MAPI%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3E'%20change%202%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20inboxFol%20%3D%20ns.GetDefaultFolder(6)%20'olFolderInbox%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20subFol%20%3D%20inboxFol.Folders(%22Operation%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3E'Finding%20the%20search%20item%20from%20Oulook%20Inbox%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EFor%20Each%20i%20In%20inboxFol.Items%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EIf%20i.Class%20%3D%2043%20Then%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20mi%20%3D%20i%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EIf%20mi.Attachments.Count%20%26gt%3B%200%20And%20InStr(mi.SenderName%2C%20%22Ahmed%22)%20Then%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EdirName%20%3D%20%22C%3A%5CWork%20Area%22%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EIf%20fso.FolderExists(dirName)%20Then%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20dir%20%3D%20fso.GetFolder(dirName)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EElse%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20dir%20%3D%20fso.Createfolder(dirName)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3E'Saving%20Attachment%20to%20a%20folder%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EFor%20Each%20at%20In%20mi.Attachments%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EIf%20Right(at.Filename%2C%204)%20%3D%20%22xlsm%22%20Then%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3Eat.SaveAsFile%20dir.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20%22Daily%20Work%20Data.xlsm%22%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ENext%20at%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3E'%20change%204%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3Emi.UnRead%20%3D%20False%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3Emi.Move%20subFol%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ENext%20i%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CEM%3E%3CSTRONG%3E'Setting%20Folder%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20oFSO%20%3D%20CreateObject(%22Scripting.FileSystemObject%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ESet%20oFolder%20%3D%20oFSO.GetFolder(%22C%3A%5CWork%20Area%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3ECreateObject(%22shell.application%22).Open%20(%22C%3A%5CWork%20Area%5CDaily%20Work%20data.xlsm%22)%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CEM%3E%3CSTRONG%3EEnd%20Sub%3C%2FSTRONG%3E%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2659995%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Contributor

Hi 

 

I have the below vba code for "downloading mail attachment based on sender name".

Some time the sender will send multiple files in a day itslelf, but I want to download first file he sent to me using the vba. (Based on time of Sending) 

What changes to be done in the code in this case. 

 

Sub DATA()
Dim ol As Object 'Outlook.Application
Dim ns As Object 'Outlook.Namespace
Dim fol As Object 'Outlook.Folder
Dim i As Object
Dim mi As Object 'Outlook.MailItem
Dim at As Object 'Outlook.Attachment
Dim fso As Object 'Scripting.FileSystemObject
Dim dir As Object 'Scripting.Folder
Dim dirName As String
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim f As Integer
' change 1
Dim inboxFol As Object 'Outlook.Folder
Dim subFol As Object 'Outlook.Folder

'Some Set Ups
Set fso = CreateObject(Class:="Scripting.FileSystemObject")
Set ol = CreateObject(Class:="Outlook.Application")
Set ns = ol.GetNamespace("MAPI")
' change 2
Set inboxFol = ns.GetDefaultFolder(6) 'olFolderInbox
Set subFol = inboxFol.Folders("Operation")

'Finding the search item from Oulook Inbox
For Each i In inboxFol.Items
If i.Class = 43 Then
Set mi = i
If mi.Attachments.Count > 0 And InStr(mi.SenderName, "Ahmed") Then
dirName = "C:\Work Area"
If fso.FolderExists(dirName) Then
Set dir = fso.GetFolder(dirName)
Else
Set dir = fso.Createfolder(dirName)
End If


'Saving Attachment to a folder
For Each at In mi.Attachments
If Right(at.Filename, 4) = "xlsm" Then
at.SaveAsFile dir.Path & "\" & "Daily Work Data.xlsm"

End If
Next at
' change 4
mi.UnRead = False
mi.Move subFol
End If
End If
Next i

'Setting Folder
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\Work Area")


CreateObject("shell.application").Open ("C:\Work Area\Daily Work data.xlsm")

 


End Sub

 

0 Replies