Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. Public Sub Extract_Outlook_Email_Attachments()
  2. Dim OutlookOpened As Boolean
  3. Dim outApp As Outlook.Application
  4. Dim outNs As Outlook.NameSpace
  5. Dim outFolder As Outlook.MAPIFolder
  6. Dim outAttachment As Outlook.attachment
  7. Dim outItem As Object
  8. Dim saveFolder As String
  9. saveFolder = "C:Usersborjax01Desktopaging reports"
  10. Dim outMailItem As Outlook.MailItem
  11. Dim inputDate As String, subjectFilter As String
  12. If Right(saveFolder, 1) <> "" Then saveFolder = saveFolder & "" & "Aging
  13. Report.out"
  14.  
  15. inputDate = InputBox("Enter date to filter the email subject", "Extract
  16. Outlook email attachments")
  17. If inputDate = "" Then Exit Sub
  18.  
  19. InputDateFilter = inputDate
  20. subjectFilter = ("VERIPRD : XXVER Veritiv Aging Report Main : PETROP01")
  21.  
  22.  
  23. OutlookOpened = False
  24. On Error Resume Next
  25. Set outApp = GetObject(, "Outlook.Application")
  26. If Err.Number <> 0 Then
  27. Set outApp = New Outlook.Application
  28. OutlookOpened = True
  29. End If
  30. On Error GoTo 0
  31.  
  32. If outApp Is Nothing Then
  33. MsgBox "Cannot start Outlook.", vbExclamation
  34. Exit Sub
  35. End If
  36.  
  37. Set outNs = outApp.GetNamespace("MAPI")
  38. Set outFolder = outNs.PickFolder
  39.  
  40.  
  41. If Not outFolder Is Nothing Then
  42. For Each outItem In outFolder.Items
  43. If outItem.Class = Outlook.OlObjectClass.olMail Then
  44. Set outMailItem = outItem
  45. If outMailItem.Subject = subjectFilter Then
  46. Debug.Print outMailItem.Subject
  47. For Each outAttachment In outMailItem.Attachments
  48. outAttachment.SaveAsFile saveFolder
  49.  
  50.  
  51. Set outAttachment = Nothing
  52.  
  53.  
  54. Next
  55. End If
  56. End If
  57. Next
  58. End If
  59.  
  60. If OutlookOpened Then outApp.Quit
  61.  
  62. Set outApp = Nothing
  63.  
  64. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement