Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. Public Sub SaveAttachments()
  2. Dim objOL As Outlook.Application
  3. Dim objMsg As Outlook.MailItem 'Object
  4. Dim objAttachments As Outlook.Attachments
  5. Dim objSelection As Outlook.Selection
  6. Dim i As Integer
  7. Dim lngCount As Integer
  8. Dim strFile As String
  9. Dim strFolderpath As String
  10. Dim strFileName As String
  11. Dim objSubject As String
  12. Dim strDeletedFiles As String
  13. ' Get the path to your My Documents folder
  14. 'strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
  15. On Error Resume Next
  16. ' Instantiate an Outlook Application object.
  17. Set objOL = CreateObject("Outlook.Application")
  18. ' Get the collection of selected objects.
  19. Set objSelection = objOL.ActiveExplorer.Selection
  20. ' The attachment folder needs to exist
  21. ' You can change this to another folder name of your choice
  22. ' Set the Attachment folder.
  23. strFolderpath = "C:UsersdemkepDocuments"
  24. ' Check each selected item for attachments.
  25. For Each objMsg In objSelection
  26. 'Set FileName to Subject
  27. objSubject = objMsg.Subject
  28. Set objAttachments = objMsg.Attachments
  29. lngCount = objAttachments.Count
  30. If lngCount > 0 Then
  31. ' Use a count down loop for removing items
  32. ' from a collection. Otherwise, the loop counter gets
  33. ' confused and only every other item is removed.
  34. For i = lngCount To 1 Step -1
  35. ' Get the file name.
  36. strFileName = objSubject & ".pdf"
  37. ' Combine with the path to the Temp folder.
  38. strFile = strFolderpath & strFileName
  39. Debug.Print strFile
  40. ' Save the attachment as a file.
  41. objAttachments.Item(i).SaveAsFile strFile
  42. Next i
  43. End If
  44. Next
  45. ExitSub:
  46. Set objAttachments = Nothing
  47. Set objMsg = Nothing
  48. Set objSelection = Nothing
  49. Set objOL = Nothing
  50. End Sub
  51.  
  52. strFileName = objSubject & "(" & i & ").pdf"
  53.  
  54. If lngCount > 1 Then
  55. strFileName = objSubject & "(" & i & ").pdf"
  56. Else
  57. strFileName = objSubject & ".pdf"
  58. End If
  59.  
  60. strFileName = objSubject & IIf(lngCount>1, "(" & i & ")", "") & ".pdf"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement