Results 1 to 21 of 21

Send multiple emails to multiple recipients

Threaded View

YasserKhalil Send multiple emails to... 01-06-2017, 09:25 AM
Kenneth Hobson Re: Send multiple emails to... 01-06-2017, 10:31 AM
YasserKhalil Re: Send multiple emails to... 01-06-2017, 11:14 AM
YasserKhalil Re: Send multiple emails to... 01-19-2017, 06:47 AM
YasserKhalil Re: Send multiple emails to... 01-22-2017, 04:16 PM
LJMetzger Re: Send multiple emails to... 01-23-2017, 01:50 PM
LJMetzger Re: Send multiple emails to... 01-23-2017, 01:53 PM
YasserKhalil Re: Send multiple emails to... 01-23-2017, 02:43 PM
YasserKhalil Re: Send multiple emails to... 01-29-2017, 03:14 PM
LJMetzger Re: Send multiple emails to... 01-29-2017, 03:44 PM
LJMetzger Re: Send multiple emails to... 01-29-2017, 04:25 PM
Kenneth Hobson Re: Send multiple emails to... 01-29-2017, 05:47 PM
YasserKhalil Re: Send multiple emails to... 01-29-2017, 11:28 PM
Kenneth Hobson Re: Send multiple emails to... 01-30-2017, 12:07 AM
YasserKhalil Re: Send multiple emails to... 01-29-2017, 11:20 PM
YasserKhalil Re: Send multiple emails to... 01-29-2017, 11:47 PM
YasserKhalil Re: Send multiple emails to... 01-30-2017, 12:53 AM
LJMetzger Re: Send multiple emails to... 01-30-2017, 10:24 AM
YasserKhalil Re: Send multiple emails to... 01-30-2017, 12:46 PM
LJMetzger Re: Send multiple emails to... 02-04-2017, 07:11 AM
YasserKhalil Re: Send multiple emails to... 02-04-2017, 09:26 AM
  1. #18
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Send multiple emails to multiple recipients

    Yasser asked: How can I add signature parts? ...point me the name of the procedure and the line at which I need to add or edit to be able to follow changes
    In post #6 See Sub SendEmailViaOutlook() in code Module ModOutlookEmail .

    The best way to answer is through an annotated example. First we will start with a relatively simple working code example. To add to the simplicity, Outlook MUST be open before the code is run from Excel.

    The example does the following:
    a. Uses the Default Outlook Account as the sender
    b. Uses the default Outlook Signature
    c. Uses a simple HTML text message
    Option Explicit
    
    Sub SimpleOutlookEmailDiplayWithTextBodyAndSignature()
      'This assumes that Outlook is already open to simplify the code
      
      'Reference: http://www.w3schools.com/html/html_paragraphs.asp
      'Reference: http://www.html.am/html-codes/text/
      
      Dim OutApp As Object
      Dim OutMail As Object
      
      Dim sHtmlBody As String
      Dim sSignature As String
      
      
     'Attempt to create an Outlook object
      On Error Resume Next
      Set OutApp = GetObject(, "Outlook.Application")
      If Err.Number <> 0 Then
        Err.Clear
        msgbox "NOTHING DONE.  The Outlook Object could not be created from Excel." & vbCrLf & _
               "Try again when Outlook is open."
        Exit Sub
      End If
      On Error GoTo 0
      
      'Create the Body of the Email
      sHtmlBody = _
        "This is line one of the email body." & "<BR>" & _
        "This is line two." & _
        "This is a continuation of line two."
    
      
      'Create the Outlook Mail Object (using the default Email account)
      Set OutMail = OutApp.CreateItem(0)
      
      
      'Display the Default Signature (If Any) - it is contained in .HTMLBody
      'If there is no default signature, nothing is displayed
      OutMail.display
      sSignature = OutMail.htmlbody
     
      'Determine the values to be sent
      With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Info Request"
        .htmlbody = sHtmlBody & sSignature
        .display
        
        '.Send - comment out the 'Display line' if you want to send
      End With
      
      'Clear the Object Pointers
      Set OutMail = Nothing
      Set OutApp = Nothing
    
    End Sub
    Now to describe what various parts of the code are used for.
    This opens Outlook only if it is already open using 'Late Binding'. For examples of 'Early Binding' vs 'Late Binding' see:http://www.excelforum.com/showthread.php?t=1020212
    Early Binding is Excel version dependent and requires use of a VBA Library reference. If moving code between computers that use different versions of Office, the other computer must explicitly open the 'Library Reference' on the new computer.
    Late Binding is version independent. However, if Outlook Constants are used, their values must be explicitly used.

    For Outlook Constants and their values see: https://msdn.microsoft.com/en-us/lib...ffice.11).aspx
    Set OutApp = GetObject(, "Outlook.Application")
    The Email body is saved in a separate string variable and can go anywhere in the code prior to the .Body or .HTMLBody statement that uses the variable. When using a Signature that contains something other than straight text, the Email body must use the HTML "<br>" as an EOL (End of Line) indicator. "<br>" is roughly equivalent to vbCrLf (Carriage Return - Linefeed) or vbLf (Linefeed) depending on the EOL convention of the computer. If the Body and Signature combination use Text only, the .Body statement is used and I use vbCrLf as the EOL character.
    Dim sBody as string
    sBody = _
          "This is line one of the email body." & vbCrLf & _
          "This is line two." & _
          "This is a continuation of line two."
    If the Body or Signature contains HTML (and/or picture in the Signature) , the .HTMLBody statement is used and "<BR>" must be used as the EOL character.
    Dim sHtmlBody As String
    sHtmlBody = _
        "This is line one of the email body." & "<BR>" & _
        "This is line two." & _
        "This is a continuation of line two."
    After the 'Outlook Application' object is created, a 'Mail Object' must be created. The 'Mail Object' references a specific account which is usually Account '0' (the Default Account). If Outlook has 5 different Email accounts, then the Email address with Account 4 can be used as shown below.
    'Create the Outlook Mail Object (using the default Email account)
    Set OutMail = OutApp.CreateItem(0)
    
    'or
    
    'Create the Outlook Mail Object (using the Email address associated with Account '4')
    Set OutMail = OutApp.CreateItem(4)
    The 'Mail Object' does the rest of the work. There is no specific VBA command to get a signature, so a little trickery is required. If the Email body is BLANK the .Display command will display the Signature associated with the 'Mail Object' (a specific Email address). The signature can then be saved for later use.
    Dim sSignature As String
    'Display the Default Signature (If Any) - it is contained in .HTMLBody
    'If there is no default signature, nothing is displayed
    OutMail.display
    sSignature = OutMail.htmlbody

    Most of the following items are self-explanatory:
      'Determine the values to be sent
    With OutMail
      .To = "a@b.com;c@d.com"
      .CC = ""    'Carbon Copy
      .BCC = ""   'Blind Carbon Copy (other recipients don't know this person sees the Email)
      .Subject = "Info Request"
    
      '...
    End With
    Combine the Email Body and the Signature
    With OutMail
      '...
      .htmlbody = sHtmlBody & sSignature
      '...
    End With
    Add attachements if any
    With OutMail
      '...
      .Attachments.Add "H:\abc.docx"
      .Attachments.Add "H:\def.docx"
      '...
    End With
    Finally,
    a. To Display the Email in Outlook for Editing and review in Outlook:
    With OutMail
      '...
      .Display
      '...
    End With
    or

    a. To Send the Email to Outlook in the 'Send Queue'. This command DOES NOT send the message. Outlook is usually configured to Send/Receive periodically or to Send/Receive on Manual Commands by the User. The Email will be ACTUALLY SENT according to the Outlook setup.
    With OutMail
      '...
      .Send
      '...
    End With
    To send the Email NOW is a topic for a completely different thread at some future time.

    Lewis
    Last edited by LJMetzger; 01-30-2017 at 10:36 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 04-17-2015, 06:01 AM
  2. Sending emails to multiple recipients through VBA
    By MikeFranz123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-17-2014, 09:47 AM
  3. [SOLVED] Macro To Send Emails with PDF: Multiple Emails and PDF's
    By totoga12 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 03-19-2014, 06:13 PM
  4. send email to multiple recipients
    By plans in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-18-2014, 09:26 AM
  5. Macro to send multiple attachments to several recipients
    By dave1983 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2013, 11:07 AM
  6. send email from excel to multiple recipients
    By hariexcel1987 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 01-13-2013, 01:41 PM
  7. Send email to multiple recipients
    By Court16 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-24-2009, 05:20 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