Consulting

Results 1 to 2 of 2

Thread: VBA Outlook single mail instead of multiple mails to recipient

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    15
    Location

    VBA Outlook single mail instead of multiple mails to recipient

    Hi

    I have the following code, which works but would like to improve.

    the purpose of the code is to send a report to multiple recipients at once, these recipients mail addresses are found on Sheet1 Column H. they are also dynamic.

    the code works fine in the sense that it opens and creates the attachment for each recipient, but it creates a new mail for each recipient. I would only like to change it so that it creates a single new mail and then places all the recipients into one e-mail.
    HTML Code:
    Dim OutApp As Object    
    Dim OutMail As Object    
    Dim cell As Range
        
    Application.ScreenUpdating = False    
    Set OutApp = CreateObject("Outlook.Application")
    
    On Error GoTo cleanup    
    For Each cell In Sheet1.Columns("H").Cells.SpecialCells(xlCellTypeConstants)        
    
    If cell.Value Like "?*@?*.?*" Then
    
    Set OutMail = OutApp.CreateItem(0)            
    
    On Error Resume Next            
    
    With OutMail                
    .To = cell.Value                
    .Subject = "Reminder"               
    .Body = "Dear "                
    .Attachments.Add Sheet1.Range("B4") & "\3. SHE Reports\" & Sheet1.Range("AP4") & " " & Sheet1.Range("AO4") & " SHE REPORT" & ".pdf"      
    .Display            
    End With            
    On Error GoTo 0            
    Set OutMail = Nothing        
    
    End If    
    Next cell
    cleanup:    
    Set OutApp = Nothing    
    Application.ScreenUpdating = True

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    assumptions:
    1 recipients' email addresses are in a contiguous range in column H, starting at H1 (ie, there is no header cell)
    2) Outlook separator is semicolon ";"

    Sub vbax_63139_multi_recipient_email()
    
        With CreateObject("Outlook.Application")
            With .CreateItem(olMailItem)
                .To = Join(Application.Transpose(Range("H1").CurrentRegion), ";")
                .Subject = "Reminder"
                .Body = "Dear "
                .Attachments.Add Sheet1.Range("B4") & "\3. SHE Reports\" & Sheet1.Range("AP4") & " " & Sheet1.Range("AO4") & " SHE REPORT" & ".pdf"
                .Display
            End With
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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