Outlook VBA Addin

Ryan Rood
Ryan Rood used Ask the Experts™
on
I found this code snippet on another site: https://www.extendoffice.com/documents/outlook/3747-outlook-auto-download-save-attachments-to-folder.html

This works! Which is great. However I would like to modify this to only grab PDF files from the emails and prepend a date to the file name (yyyymmdd - original filename). I have zero programming knowledge. I have found a few different sites and have tried to modify but it just breaks the code and the script stops working. Any assistance would be appreciated.

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Users\DT168\Documents\outlook-attachments\"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

pls try
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
Dim oAttachment As Outlook.Attachment
Dim sSaveFolder As String
sSaveFolder = "C:\Users\DT168\Documents\outlook-attachments\"
For Each oAttachment In MItem.Attachments
    If oAttachment.FileName Like "*.pdf" Then
        oAttachment.SaveAsFile sSaveFolder & Format(Date, "yyyymmdd") & oAttachment.DisplayName
    End If
Next
End Sub

Open in new window

Regards

Author

Commented:
Thank you so much! That is exactly what I needed! :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial