+ Reply to Thread
Results 1 to 3 of 3

Excel VBA Bulk Email with date and time email sent

Hybrid View

  1. #1
    Registered User
    Join Date
    10-31-2019
    Location
    Montreal
    MS-Off Ver
    2016
    Posts
    19

    Excel VBA Bulk Email with date and time email sent

    Hi guys,

    I am working an Excel file to send bulk emails to clients from a list.

    I made this VBA for only the first row of the list but I don't know how to make it send emails to the whole list. Values that change are K2, A2 and B2.

    I also would like to add the column to indicates that the email was sent with the date and time.

    Your help is really appreciated!

    Thank you so much
    Claudia

    Sub ESES()
    
    Dim emailApplication As Object
    Dim emailItem As Object
    
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItemFromTemplate(Range("K2").Text)
    
    emailItem.SendUsingAccount = "myemail@email.com"
    
    emailItem.to = Range("B2").Value
    emailItem.Subject = "Business Appointment"
    
    Dim sBody As String
    
    sBody = emailItem.HTMLbody
    sBody = Replace(sBody, "#$ClientNumber#", Range("A2").Text)
    emailItem.HTMLbody = sBody
    
    emailItem.Display
    
    Set emailItem = Nothing
    Set emailApplication = Nothing
    
    End Sub
    Last edited by ClaudiaRovastino; 05-18-2020 at 08:13 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Excel VBA Bulk Email with date and time email sent

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Excel VBA Bulk Email with date and time email sent

    Hi Claudia,

    Try the following code. I tested the code the best that I could, however I had to take a few shortcuts because I do not use Outlook templates.
    Option Explicit
    
    Sub ESES()
    
      Dim wb As Workbook
      Dim ws As Worksheet
    
      Dim emailApplication As Object
      Dim emailItem As Object
      
      Dim iLastRow As Long
      Dim iRow As Long
    
      Dim sBody As String
      Dim sClientNumber As String
      Dim sTemplatePathAndFileName As String
      Dim sRecipient As String
      
      'Create Worksheet Object
      Set wb = ThisWorkbook          'The file that contains this code
      Set ws = wb.Sheets("Sheet1")   'Change this to your Sheet Name
      
      'Create the Outlook Object
      Set emailApplication = CreateObject("Outlook.Application")
      
      
      'Find the Last Blank Row in Column 'B'
      iLastRow = ws.Columns("B").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
      
      'Process emails until Column 'B' is BLANK
      For iRow = 2 To iLastRow
      
        'Get the Data For this Row  (remove Leading/Trailing Spaces)
        '.text gets item exactly as displayed ('####' if column is too narrow)
        '.value same as .value2 except when Currency or Date returns VBA Currency (truncated) or VBA Date
        '.value2 gets item as stored by Excel
        sClientNumber = Trim(ws.Cells(iRow, "A").Value)
        sRecipient = Trim(ws.Cells(iRow, "B").Value)
        sTemplatePathAndFileName = Trim(ws.Cells(iRow, "K").Value)
        
        'Get the Email Object
        Set emailItem = emailApplication.CreateItemFromTemplate(sTemplatePathAndFileName)
    
        emailItem.SendUsingAccount = "myemail@email.com"
    
        emailItem.to = sRecipient
        emailItem.Subject = "Business Appointment"
    
        sBody = emailItem.HTMLbody
        sBody = Replace(sBody, "#$ClientNumber#", sClientNumber)
        emailItem.HTMLbody = sBody
    
        emailItem.Display
        'emailItem.Send     'Use this to Send without preview
      
        'Output Date And Time (several Examples)
        ws.Cells(iRow, "X").Value = Format(Now(), "dddd mmmm d, yyyy h:mm AM/PM")
        ws.Cells(iRow, "Y").Value = Format(Now(), "mmm d, yyyy h:mm AM/PM")       'Column Y displays date in underlying Numberformaat for that column
        ws.Cells(iRow, "Z").Value = Now()
        ws.Cells(iRow, "Z").NumberFormat = "mmm d, yyyy hh:mm AM/PM"
      
      
        'Clear the Email Item Object
        Set emailItem = Nothing
        
      Next iRow
      
      'AutoFormat the Date Columns
      ws.Columns("X:Z").Columns.AutoFit
      
      
      'Clear Object Pointers
      Set wb = Nothing
      Set ws = Nothing
      
      Set emailItem = Nothing
      Set emailApplication = Nothing
    
    End Sub
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. Dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Unable to send bulk email from excel
    By Sona85 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-20-2019, 12:12 AM
  2. [SOLVED] how to check bulk email address is correct or not in excel file
    By johnquazi in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-23-2018, 12:17 AM
  3. [SOLVED] Need to send bulk Email from excel file
    By Sanjibghosh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-12-2018, 07:36 AM
  4. VB Code to email bulk of files
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-31-2015, 07:11 AM
  5. Bulk Email Addresses to Excel
    By daw007 in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 04-22-2013, 12:27 AM
  6. Adding commas "," in email addresses for sending BULK Email stored cellwise..!
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-26-2011, 09:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1