[Solved] Save attachment form specific Sender or Subject to a Folder

bigme

Member
dear friends,
is it possible using VBA to save an email's attachment from specific sender or subject to a folder? kindly help me how to do it, thank you.

regards,
bigMe
 
dear friends,
is it possible using VBA to save an email's attachment from specific sender or subject to a folder? kindly help me how to do it, thank you.

regards,
bigMe
Hey bigMe, I have two types of solution to your problem. The first code here will save the attachments from the sender automatically in the folder path you specify.
You need to open the Outlook app and enable Macros for it.
  • Next, press Alt + F11 to open the VBA Editor window.
  • After that, insert the code below under ThisOutlookSession private module.
Public WithEvents objInboxItems As Outlook.Items Private Sub Application_Startup() Set objInboxItems = Session.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub objInboxItems_ItemAdd(ByVal item As Object) Dim mn_MailObj As Outlook.MailItem Dim mn_SenderAddress As String Dim mn_SenderDomain As String Dim objAttachment As attachment Dim mn_FolderPath As String Dim mn_DesiredSender As String mn_FolderPath = "C:\Folder Path\" mn_DesiredSender = "[email protected]" If item.Class = olMail Then Set mn_MailObj = item mn_SenderAddress = mn_MailObj.SenderEmailAddress mn_SenderDomain = Right(mn_SenderAddress, Len(mn_SenderAddress) - InStr(mn_SenderAddress, "@")) If mn_SenderAddress = mn_DesiredSender Then If mn_MailObj.Attachments.Count > 0 Then For Each objAttachment In mn_MailObj.Attachments objAttachment.SaveAsFile mn_FolderPath & objAttachment.FileName Next End If End If End If End Sub
View attachment 967
Insert the folder path where you want to save the attachment and sender email address properly. You need to save the Outlook file and restart it. Now, when you receive any attachments from the sender, the attachment will automatically be saved in the desired file location.
Here is the another code if you want to save the attachments manually. Just open a VBA Module and insert the code into it.
Sub SaveAttachmentsFromSpecificSender() Dim mn_namesapace As NameSpace Dim mn_inbox As MAPIFolder Dim mn_item As Object Dim attachment As attachment Dim mn_senderAddress As String Dim mn_saveFolder As String mn_senderAddress = "[email protected]" mn_saveFolder = "C:\Folder Path\" Set mn_namesapace = Application.GetNamespace("MAPI") Set mn_inbox = mn_namesapace.GetDefaultFolder(olFolderInbox) For Each mn_item In mn_inbox.Items If TypeOf mn_item Is MailItem Then If mn_item.SenderEmailAddress = mn_senderAddress Then For Each attachment In mn_item.Attachments attachment.SaveAsFile mn_saveFolder & attachment.FileName Next attachment End If End If Next mn_item Set mn_namesapace = Nothing Set mn_inbox = Nothing Set mn_item = Nothing Set attachment = Nothing End Sub
After running the code, you will see the attachments from the specific sender in the desired file location.
 

Online statistics

Members online
0
Guests online
4
Total visitors
4

Forum statistics

Threads
371
Messages
1,627
Members
705
Latest member
curioso
Back
Top