+ Reply to Thread
Results 1 to 12 of 12

Selecting Multiple Email Addresses

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    29

    Selecting Multiple Email Addresses

    Can anyone help with modifying this code to select multiple email addresses that are in a list in excel rather than having to hard code an email into VBA?

    Sub Send_Range()

    ' Select the range of cells on the active worksheet.
    ActiveSheet.Range("A1:B5").Select

    ' Show the envelope on the ActiveWorkbook.
    ActiveWorkbook.EnvelopeVisible = True

    ' Set the optional introduction field thats adds
    ' some header text to the email body. It also sets
    ' the To and Subject lines. Finally the message
    ' is sent.
    With ActiveSheet.MailEnvelope
    .Introduction = "This is a sample worksheet."
    .Item.To = "E-Mail_Address_Here"
    .Item.Subject = "My subject"
    .Item.Send
    End With
    End Sub

    Thanks!

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,018

    Re: Selecting Multiple Email Addresses

    .
    Here's an example to work with that includes attachments as well listed in Col B:

    'Option Explicit
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Sub SendMultipleEmails()
    On Error Resume Next
    
    Dim Mail_Object, OutApp As Variant, lastRow As Variant
    Dim i As Integer
    Dim sht As Sheet1
    
    Set sht = ThisWorkbook.Worksheets("Sheet1")
    
        lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
      
    
    For i = 2 To lastRow
    
    Set Mail_Object = CreateObject("Outlook.Application")
    Set OutApp = Mail_Object.CreateItem(0)
    
        With OutApp
        .Subject = "Testing MultiEmails"
        .Body = "Hello TEST !"
        .To = Cells(i, 1).Value
        .Attachments.Add Cells(i, 2).Value
        '.send
        .Display
        End With
            
        If i = lastRow Then
            GoTo Done
        Else
            'Calculate
           ' Sleep (5000) ' delay 1 second
        End If
    Next i
       
    
    debugs:
    If Err.Description <> "" Then MsgBox Err.Description
    
    Done:
    
    MsgBox "All emails have been sent. "
    End Sub

  3. #3
    Registered User
    Join Date
    08-17-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    29

    Re: Selecting Multiple Email Addresses

    Thanks I was trying to use the envelope feature to create and send the email because I would ultimately like to copy a range of cells and paste special the picture of that copy into the email.

    I already have the code below that works with the email list and does everything like I expect it to but I can't seem to get the same concept to work with the envelope feature.

    Maybe the question I should be asking is how can I paste special a picture of a copied range of cells into the email in the code below?

    
    Sub SendRAILRequestEmail()
    '
    ' SendRAILRequestEmail Macro
    '
    'create variables for string data
    Dim Qdate As String
    Dim Qowner As String
    Dim Qaction As String
    Dim Qproblem As String
    Dim dataBook As Workbook 'workbook object for the data storing workbook
    Dim dataBookSheet As Worksheet 'object for working with the data collecting workbooks "Recognition Sharing" worksheet
    Dim usedRowCount As Double 'set row counter for finding next blank row
    Dim emailCount As Double 'set a variable for finding number of email names in "Email List"
    
    'set variables for email message setup
    Dim aOutlook As Object
    Dim aEmail As Object
    Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
    
    'copy data from blocks into variable
    Qdate = Worksheets("Rail Entry").Cells(6, 11).Value
    Qowner = Worksheets("Rail Entry").Cells(6, 5).Value
    Qaction = Worksheets("Rail Entry").Cells(6, 8).Value
    Qproblem = Worksheets("Rail Entry").Cells(6, 7).Value
    
    Worksheets("Email List").Activate 'activate email list worksheet to work with
    
    'code for email message
    Set aOutlook = CreateObject("Outlook.Application")
     Set aEmail = aOutlook.CreateItem(0)
     'find number of email names in list on "Email List" sheet set in column 1 starting on row 2 (title on row 1)
     emailCount = Application.WorksheetFunction.CountA(Columns("A:A"))
     emailCount = emailCount - 2 'make an adjustment to the email count list to account for the title cell and starting range of cells
        If emailCount < 0 Then 'if statement to disable email sending if no addresses are in the list
        MsgBox ("The email address list is empty please insert an email address, data will be saved normally, continuing with program")
       Else
        'set sheet to find address for e-mails as I have several people to mail to
        Set rngeAddresses = ActiveSheet.Range(Cells(2, 1), Cells(2 + emailCount, 1))
        For Each rngeCell In rngeAddresses.Cells
        strRecipients = strRecipients & ";" & rngeCell.Value
        Next
        'Set Subject
        aEmail.Subject = "Pryor Rail Follow up for Effectiveness Request"
        
        'Set Body for mail
        aEmail.Body = "On " & Qdate & " " & Qowner & " took action to " & "(" & Qaction & ")" & _
                     " to address the problem of " & "(" & Qproblem & "." & ")" & Chr(10) & Chr(10) & _
                    "You have been requested to follow up on this action and check for effectiveness. " & _
                    "Please evaluate this action and determine if it was effective and follow up with " & Qowner & " then update the rail log. " & Chr(10) & _
                    "This was an automatic email generated by the Pryor HBS Log Workbook"
     
        
        'Set Recipient
        aEmail.To = strRecipients
        'or send one off to 1 person use this static code
        'aEmail.Recipients.Add "[email protected]"
        'Send the Email
        aEmail.Send
    End If
    
    ' Copy emails from the currently used list and paste in the recently used email list
        
        Application.Goto Reference:="EMAILCOPY"
        Selection.Copy
        Range("I1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1, 0).Select
        ActiveSheet.Paste
        Range("A2").Select
            
        'Empty emails used for this recognition
        Application.Goto Reference:="EMAILS"
        Selection.ClearContents
        
        Application.Goto Reference:="WorkbookHome"
        Application.CutCopyMode = False
        ActiveWorkbook.Save
        
    
    End Sub

    Also on another note when sharing code in the message how do I get it into the separate box like you did?

    THANKS!
    Last edited by uvebeenwarrened; 08-17-2017 at 05:29 PM.

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Selecting Multiple Email Addresses

    Welcome to the forum!

    Drop Envelope method.

    Be sure to download Ron's routine as commented.

    e.g.
    Sub SendToOutlook1()
      Dim olApp As Outlook.Application, olMail As Outlook.MailItem
      Dim sig$
      
      Set olApp = New Outlook.Application
      Set olMail = olApp.CreateItem(olMailItem)
      With olMail
        .Display
        sig = .HTMLBody
        .To = "[email protected]"
        .Subject = "Restaurant Summary Report"
        'http://www.rondebruin.nl/win/s1/outlook/bmail2.htm
        .HTMLBody = RangetoHTML([B6:R136]) & sig
        .Display
        '.Send
      End With
      Set olMail = Nothing
      Set olApp = Nothing
    End Sub

  5. #5
    Registered User
    Join Date
    08-17-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    29

    Re: Selecting Multiple Email Addresses

    Thanks Ken. I like the Drop Envelope method other than I would like the email addresses to be selected from a list in excel rather than having to hard code them into VBA every time they change and I can't seem to figure out how to make that work. Plus I will want to send to multiple emails at once.

    What do you mean download Ron's routine as commented?

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Selecting Multiple Email Addresses

    RangeToHTML() is a custom routine. No sense posting it since Ron does on his site.

  7. #7
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,018

    Re: Selecting Multiple Email Addresses

    .
    When you click on the HASH MARK symbol, it creates this : (CODE)(/CODE)"

    First click on the HASH symbol, then create a blank line between the two so it looks like this:

    (CODE)

    (/CODE)

    Then paste your code in the blank line.

    The words CODE and /CODE will actually be surrounded by these symbols [ ] . I couldn't use those in the explanation above
    because it would not have displayed correctly for the explanation. Here's an example of what I mean :


    You don't actually see the words CODE & /CODE


    To the point of your question .. let me review the code. I've not had much luck with sending email via ENVELOPE method.

  8. #8
    Registered User
    Join Date
    08-17-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    29

    Re: Selecting Multiple Email Addresses

    Thanks!

     
    Just testing

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,018

    Re: Selecting Multiple Email Addresses

    .
    Here is an example and sample workbook.

    Option Explicit
    Sub SendEmail()
        
        Dim sCC As String, sSubj As String, sEmAdd As String
         
         '// Change the values of these variables to suit
        sEmAdd = Range("C1")
        sCC = ""
        sSubj = Range("C2")
    
        With Application
            .EnableEvents = 0
            .ScreenUpdating = 0
            .Calculation = xlCalculationManual
        End With
        
        On Error Resume Next
        With CreateObject("Outlook.Application").CreateItem(0)
            .To = sEmAdd
            .CC = sCC
            .Subject = sSubj
            .HTMLBody = RangetoHTML(Sheets("To Csh Mgt").Range("c7:b33"))
            '.Send '// Change this to .Display if you want to view the email before sending.
            .display
        End With
        On Error GoTo 0
         
        With Application
            .EnableEvents = 1
            .ScreenUpdating = 1
            .Calculation = xlCalculationAutomatic
        End With
    End Sub
     
    Function RangetoHTML(rng As Range)
        Dim TempWB As Workbook, TempFile As String
        TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteColumnWidths, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
        End With
        With TempWB.PublishObjects.Add( _
            SourceType:=xlSourceRange, _
            Filename:=TempFile, _
            Sheet:=TempWB.Sheets(1).Name, _
            Source:=TempWB.Sheets(1).UsedRange.Address, _
            HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
        With CreateObject("Scripting.FileSystemObject").GetFile(TempFile).OpenAsTextStream(1, -2)
            RangetoHTML = .readall
            .Close
        End With
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
        "align=left x:publishsource=")
         
        TempWB.Close 0
        Kill TempFile
         
        Set TempWB = Nothing
    End Function
    Attached Files Attached Files

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Selecting Multiple Email Addresses

    To get back to your original question, it depends on your data. Is the range of emails in a row, column, both, a listbox, etc.

  11. #11
    Registered User
    Join Date
    08-17-2017
    Location
    USA
    MS-Off Ver
    2016
    Posts
    29

    Re: Selecting Multiple Email Addresses

    Thanks Logit. I'm going to try to incorporate some of this code and see if I can get it to work.

    Ken-Currently the email addresses are manually typed into a column. The same starting point for the list is used all the time but the emails themselves of the number of emails change every time.
    Last edited by uvebeenwarrened; 08-17-2017 at 05:34 PM.

+ 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. Replies: 3
    Last Post: 11-29-2014, 07:30 AM
  2. Multiple email addresses in a cell for email merge use
    By selinaang3012 in forum Excel General
    Replies: 0
    Last Post: 03-11-2013, 05:59 AM
  3. sending email by selecting from a lsit of addresses
    By modytrane in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-03-2010, 01:13 PM
  4. email excel file or worksheet as an attachment to multiple email addresses
    By jgeagle5 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2009, 03:40 PM
  5. Sending an email from Excel to multiple email addresses
    By insanity66 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2009, 02:01 PM
  6. Replies: 5
    Last Post: 07-23-2009, 03:01 AM
  7. edit, save as new and email to multiple email addresses
    By murphyx232 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-20-2007, 02:37 PM

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