1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
Sub Extract_Attachments_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
Dim olItems As Outlook.MailItem
Dim olName As Outlook.MailItem
Dim olNs As Outlook.NameSpace
Dim olTaskFolder As Outlook.MAPIFolder
Dim olTask As Outlook.TaskItem
Dim oPath As String
On Error Resume Next
'// Initial Shell Scriptit Instance
Set objWSCript = CreateObject("WScript.Shell")
Set olNs = GetNamespace("Mapi")
Set olTaskFolder = olNs.GetDefaultFolder(olFolderInbox)
Set olItems = olTaskFolder.Items
Set olName = olItems.Sender
'// Get My Document folder Path
sFolderPath = objWSCript.SpecialFolders(16)
'// Get the Selection
Set olSelection = ActiveExplorer.Selection
'// Set Where the attachements will be saved
oPath = sFolderPath & "\" & "Outlook Attachments" & "\" & olName & "\"
If Dir(oPath, vbDirectory) = 0 Then
MkDir path:=oPath
End If
'______________________________________________________
' Extract Attachments
'______________________________________________________
'// Looping all the mail items from selection
For Each olMail In olSelection
Set olAtchs = olMail.Attachments
iCount = olAtchs.Count '//Attachement count on mail item
sDeletedFiles = ""
'//If there are attachments
If iCount > 0 Then
For i = iCount To 1 Step -1
sFilePath = oPath & olAtchs.Item(i).FileName
olAtchs.Item(i).SaveAsFile sFilePath
'// Optional: To delete the attachments
'olatchs.Item(i).delete
'// Modify mail body 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 saved 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 |
Partager