Advertisement
dave3009

Dee_EFF

May 16th, 2024
1,441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 3.09 KB | Software | 0 0
  1. Option Explicit
  2.  
  3. '************************************************
  4. '*                                              *
  5. '* AutoEmail Function for Excel for Freelancers *
  6. '* Author: David Houston                        *
  7. '* Date: 16.05.2024                             *
  8. '*                                              *
  9. '************************************************
  10.  
  11. Const FromEmail As String = "whateveremail@email.com"       ' Set the From email address
  12. Const ccEmail As String = "whateveremail@email.com"         ' Set the CC email address
  13.  
  14.  
  15.  
  16. Sub AutoEMail()
  17.  
  18. Dim OutlookApp As Outlook.Application
  19. Dim OutlookMail As MailItem
  20. Dim Outlook
  21. Dim CustEmailRange As Range
  22. Dim Cell As Range
  23. Dim oSubject As String
  24.  
  25. ' Set up a holder for the subject line, only "No AP Listed" will change
  26. oSubject = Sheet1.Cells(Cell.Row, 46).Value & " (" & Sheet1.Cells(Cell.Row, 3).Value & " / No AP Listed )"
  27.  
  28. Set OutlookApp = New Outlook.Application                    ' Create new instance of Outlook
  29. Set CustEmailRange = Sheet1.Range("A1:A100")                ' Define the range containing the customer email details.
  30.  
  31. For Each Cell In CustEmailRange                             ' Loop over the customer email range
  32.  
  33.     Set OutlookMail = OutlookApp.CreateItem(olMailItem)         ' Create a New Email
  34.    
  35.         ' If column 36 has a value, replace "No AP Listed" in the subject line
  36.        If Sheet1.Cells(Cell.Row, 36).Value <> "" Then
  37.             oSubject = Replace(oSubject, "No AP Listed", Sheet1.Cells(Cell.Row, 36).Value)
  38.         End If
  39.        
  40.         With OutlookMail
  41.             .To = Cell.Value                                        ' Set the To address
  42.            .SentOnBehalfOfName = FromEmail                         ' Set the From Address
  43.            .CC = ccEmail                                           ' Set the CC address
  44.            .Subject = oSubject                                     ' Set the Subject
  45.            .Body = EmailBody                                       ' Set the body from function
  46.            .Display                                                ' Display the completed email, change to .Send if desired
  47.        End With
  48.    
  49.     Set OutlookMail = Nothing                                   ' Unset the mail item, not necessary
  50.  
  51. Next Cell
  52.  
  53. Set OutlookApp = Nothing                                    ' Unset Outlook
  54.  
  55. End Sub
  56.  
  57. Function EmailBody()
  58. EmailBody = Sheets("InitialEmailBody").Range("EmailBody").Value & vbCrLf
  59. EmailBody = EmailBody & "Review ID: " & Sheet1.Cells(Cell.Row, 3).Value & vbCrLf
  60. EmailBody = EmailBody & "ReviewOrder: " & IIf(Sheet1.Cells(Cell.Row, 4).Value = "", "Not Listed", Sheet1.Cells(Cell.Row, 4).Value) & vbCrLf
  61. EmailBody = EmailBody & "Review Plan: " & IIf(Sheet1.Cells(Cell.Row, 36).Value = "", "Not Listed", Sheet1.Cells(Cell.Row, 36).Value) & vbCrLf
  62. EmailBody = EmailBody & "Review Name: " & Sheet1.Cells(Cell.Row & Range("TEST").Value) & vbCrLf
  63. EmailBody = EmailBody & "Review Description: " & Sheet1.Cells(Cell.Row, 9).Value & vbCrLf
  64. EmailBody = EmailBody & "Link: " & Sheet1.Cells(Cell.Row, 42).Value & vbCrLf
  65. End Function
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement