Save attachment from outlook to a specific folder on computer, sent by a specific sender and save the file (both excel and pdf) with date & time stamp

vckothari

New Member
Joined
Sep 16, 2021
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
We on a daily basis receive emails with attachments from our custodian and that needs to be saved in a folder which represent year say 2021(already created), in the year, which month say Sep21 and in that folder a sub folder which has both monthly and daily. The next is the file name should have the date, time incorporated before its saved. I am fine if the folders cannot be created automatically but if the latter (file) can be saved with all name details in a common folder i a fine. The issue is that the email has both types of file xlsx & pdf and second is getting file name changed while saving. So technically you receive the email, both attachment gets saved in a common folder and (simple one) its renamed with current timings and date.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This might help you. "Time" is not allowed in filenames since it contains ":"
So I just changed the name to the date of today.

Note: you run this code from the Excel VBA editor.

VBA Code:
Sub fmail()
fpath = "C:\Users\xxx\Downloads\"
 
 With CreateObject("outlook.application").GetNamespace("MAPI").GetDefaultFolder(6)
    For Each it In .items
      If it.Sender = "Name of sender" Then
        For Each att In it.attachments
           att.SaveAsFile fpath & Date & Mid(att, InStr(Len(att) - 6, att, "."))
        Next
      End If
    Next
 End With
End Sub
 
Upvote 0
This might help you. "Time" is not allowed in filenames since it contains ":"
So I just changed the name to the date of today.

Note: you run this code from the Excel VBA editor.

VBA Code:
Sub fmail()
fpath = "C:\Users\xxx\Downloads\"
 
 With CreateObject("outlook.application").GetNamespace("MAPI").GetDefaultFolder(6)
    For Each it In .items
      If it.Sender = "Name of sender" Then
        For Each att In it.attachments
           att.SaveAsFile fpath & Date & Mid(att, InStr(Len(att) - 6, att, "."))
        Next
      End If
    Next
 End With
End Sub
Thank buddy..the email will have attachments pdf and excel, will this code cater to both ? Why need to run this code on excel , should it be not from outlook ? I have seen file taking date and time from the system and saving it as name…
 
Upvote 0
It saves Excel Files as xlxs and PDF files as .pdf. That is what you want right?
You can change the name of the files as you like. If you use NOW instead of DATE you will get an error because of illegal characters in your filename.
 
Upvote 0
To "timestamp" the file you could use Format, like in

VBA Code:
myVar = Format(Now, "yyyy-mm-dd_hh-mm-ss")
Use myVar instead of Date in your macro

Hth
 
Upvote 0
It saves Excel Files as xlxs and PDF files as .pdf. That is what you want right?
You can change the name of the files as you like. If you use NOW instead of DATE you will get an error because of illegal characters in your filename.
Yes i need to save both pdf and excel as is in format wise. But just confirm why do i need to run from excel and if yes I am clue less how will it trigger evertime that email comes. I will try this code..
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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