Outlook VBA Code
My problem is that I have individuals that are sending me files with the exact same name and I need to make sure they do not write over each other.
So I'm using the below code to auto add attachments to file location. However, I need to modify it so that each file has a unique file name.
I'm still a VBA novice so I need someone to walk me through the edit.
Thanks in advance.
Eric
My problem is that I have individuals that are sending me files with the exact same name and I need to make sure they do not write over each other.
So I'm using the below code to auto add attachments to file location. However, I need to modify it so that each file has a unique file name.
I'm still a VBA novice so I need someone to walk me through the edit.
Thanks in advance.
Eric
Code:
Option Explicit
Sub Extract_Attachemnts_From_Selection()
Dim OlMail As MailItem
Dim OlAtchs As Attachments
Dim OlSelection As Selection
Dim icount As Long, i As Long
Dim sfolderpath As String, sFilepath As String, sdeletedFiles As String
Dim objWSCript As Object '// Shell Scripting
On Error Resume Next
'// initial shell script instance
Set objWSCript = CreateObject("WSCript.Shell")
'// get my document folder Path
sfolderpath = objWSCript.specialfolders("Documents")
'// get the selection
Set OlSelection = ActiveExplorer.Selection
'// Set Where the Attachments will be saved
sfolderpath = sfolderpath & "C:\Users\(1)\COAs\"
'---------------------------------------------------
' Extract Attachments
'---------------------------------------------------
'// Looping all the mail itmes from selection
For Each OlMail In OlSelection
Set OlAtchs = OlMail.Attachments
icount = OlAtchs.Count '//Attachment count based on mail item
sdeletedFiles = ""
'//if there are attachments
If icount > 0 Then
For i = icount To 1 Step -1
sFilepath = sfolderpath & OlAtchs.Item(i).FileName
OlAtchs.Item(i).SaveAsFile sFilepath
'// optional: To delete the attachments
'olatchs.Item(i).Delete
'// Modify mail body message with note indicating where the attachments are saved
If OlMail.BodyFormat <> olFormatHTML Then
sdeletedFiles = sdeletedFiles & vbNewLine & "<file://" & sFilepath & ">"
Else
sdeletedFiles = sdeletedFiles & "<br>" & "<a href='file://" & _
sFilepath & "'>" & sFilepath & "</a>"
End If
Next i
If OlMail.BodyFormat <> olFormatHTML Then
OlMail.Body = vbNewLine & "The file(s) were save to " & sdeletedFiles & vbNewLine
Else
OlMail.HTMLBody = "<p>" & "the file(s) were saved to " & sdeletedFiles & "</p>" & OlMail.HTMLBody
End If
OlMail.Save
End If
Next OlMail
Door:
Set objWSCript = Nothing
Set OlAtchs = Nothing
Set OlSelection = Nothing
End Sub