简体   繁体   中英

Outlook VBA Save Email Attachments with Received Time and Sender Name

Problem: Saving multiple attachments from email with same file name only saves one attachment to folder

Possible Solution: Add received time and sender's name to new file name I have some VBA code that I found on the internet that I have been using and works great. I'm sorry if this question has been asked already but I have been researching it for weeks and it seems like there are multiple ways to do this and not all work with my code. I don't want to run a rule script if possible.

I use the VBA code below is being used in Outlook 365. How the code works is it saves email attachments to a specific folder on emails that I have selected in Outlook. The problem that I am running into is that macro doesn't pick up attachments with the same name. For example, I will have multiple attachments called "image.pdf" but it only saves one attachment with that name and file type. I'm thinking if I can add the Received Date and Time as well as the Sender's Name to the file name it would help make the file name unique and would save all the attachments. However, when I try this with code I think will work I get errors. Below is the code I am using. It involves two macros. The macro called "Save_Emails_TEST" finds the folder I have designated and then calls on the "SaveAttachments" macro that actually saves the attachments.

Request: Can someone please help me add the received date and time, senders' name, and original file name as the new file name that is saved in my folder?

Thank you in advance!

Ryan

Public Sub Save_Emails_TEST()
strFolderpath = "H:\Saved Email Attachments\Test\"
SaveAttachments
End Sub

Private Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String

    On Error Resume Next
    Set objOL = Application

    Set objSelection = objOL.ActiveExplorer.Selection

    ' Check each selected item for attachments.
    For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.count
        
    If lngCount > 0 Then
    
    ' Use a count down loop for removing items
    ' from a collection. Otherwise, the loop counter gets
    ' confused and only every other item is removed.
    
    For i = lngCount To 1 Step -1
    
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
    
    ' Combine with the path to the folder.
    strFile = strFolderpath & strFile
    
    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile
    
    Next i
    End If
    
    Next
    
ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub
Dim date_now As Date
Dim dateStamp As String
Dim LRandomNumber As Integer

For i = lngCount To 1 Step -1
    
    ' Get the file name.
    strFile = objAttachments.Item(i).FileName
    
    LRandomNumber = Int((300 - 200 + 1) * Rnd + 200)
    date_obj =  objMsg.ReceivedTime ' Now()
    dateStamp = Format(date_obj, "yyyy-mm-dd-hh-mm-ss")

     ' Combine with the path to the folder.
    strFile = strFolderpath & dateStamp & LRandomNumber & strFile

    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile

Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM