AlbinoRyno88
New Member
- Joined
- Dec 14, 2017
- Messages
- 1
Hi Everyone,
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 use the VBA code below in Outlook. 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.
My request is this. 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
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 use the VBA code below in Outlook. 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.
My request is this. 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
VBA Code:
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