Advertisement
YasserKhalil2019

T3795_Send Gmail Attach Workbook File By SaveCopyAs CDO

Aug 25th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. https://excel-egy.com/forum/t3795
  2. ---------------------------------
  3.  
  4. 'https://www.google.com/settings/security/lesssecureapps
  5. 'Enable Less Secure Apps (ON)
  6. '-------------------------------------------------------
  7.  
  8. Sub Send_Gmail_Attach_Workbook_File_By_SaveCopyAs_Using_CDO()
  9. Dim cdoMsg As Object
  10. Dim strTo As String
  11. Dim strSubj As String
  12. Dim strMsg As String
  13. Dim strCC As String
  14. Dim strBCC As String
  15. Dim strBody As String
  16. Dim userEmail As String
  17. Dim password As String
  18. Dim file As String
  19. Dim cdoNS As String
  20.  
  21. 'Email Information
  22. strTo = "Type The Email That You Will Send The Message To"
  23. strSubj = "This Is The Subject Line"
  24. strMsg = ""
  25. strCC = ""
  26. strBCC = ""
  27. strBody = "Hello My Friend" & vbCrLf & "Here Are The Required Attachment"
  28.  
  29. 'Gmail Account Information
  30. userEmail = "Your GMAIL"
  31. password = "Your Password"
  32.  
  33. file = Environ$("temp") & "\HR-1_" & Format(Date, "yyyymmdd") & ".xlsm"
  34. ThisWorkbook.SaveCopyAs file
  35.  
  36. cdoNS = "http://schemas.microsoft.com/cdo/configuration/"
  37. Set cdoMsg = CreateObject("CDO.Message")
  38.  
  39. With cdoMsg
  40. .To = strTo
  41. .Subject = strSubj
  42. .From = userEmail
  43. .CC = strCC
  44. .BCC = strBCC
  45. .TextBody = strBody
  46. .AddAttachment file
  47.  
  48. With .Configuration.Fields
  49. .Item(cdoNS & "smtpusessl") = True
  50. .Item(cdoNS & "smtpauthenticate") = 1
  51. .Item(cdoNS & "sendusername") = userEmail
  52. .Item(cdoNS & "sendpassword") = password
  53. .Item(cdoNS & "smtpserver") = "smtp.gmail.com"
  54. .Item(cdoNS & "sendusing") = 2
  55. .Item(cdoNS & "smtpserverport") = 465
  56. .Item(cdoNS & "smtpconnectiontimeout") = 60
  57. .Update
  58. End With
  59.  
  60. .Send
  61. End With
  62.  
  63. MsgBox "Message Sent", 64
  64. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement