+ Reply to Thread
Results 1 to 12 of 12
  1. #1
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Unhappy Multiple email recipients from excel

    Hi,

    I have obtained the following code which works great apart from the fact that I want to just send one email to all addressees instead of multiple emails. Is there someone who could help modify it please;

    Code:
    Sub TestFile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim cell As Range
        Dim strbody As String
    
        Application.ScreenUpdating = False
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
    
        On Error GoTo cleanup
        For Each cell In Range("G1:G20")
            strbody = strbody & cell.Value & vbNewLine
        Next
        For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
            If cell.Value Like "?*@?*.?*" And _
               LCase(Cells(cell.Row, "C").Value) = "yes" Then
    
                Set OutMail = OutApp.CreateItem(0)
                On Error Resume Next
                With OutMail
                    .BCC = cell.Value
                    .Subject = "Reminder"
                    .Body = "Dear " & Cells(cell.Row, "A").Value & vbNewLine & vbNewLine & strbody
                    'You can add files also like this
                    '.Attachments.Add ("C:\test.txt")
                    .Display  'Or use Send
                End With
                On Error GoTo 0
                Set OutMail = Nothing
            End If
        Next cell
    
    cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
    End Sub
    I appreciate any help offered

    Kind regards

    Steve
    Last edited by Richard Buttrey; 01-23-2010 at 12:50 PM. Reason: Missing Code tags

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Multiple email recipients from excel

    Hi Steve,

    Welcome to the forum.

    Please note the forum rules about putting VBA code inside tags. As this is your first visit I have changed them for you this time.

    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Multiple email recipients from excel

    Steve,

    Try the following modified procedure.

    Code:
    Sub TestFile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim cell As Range
        Dim strbody As String
        Dim stemails As String
    
        Application.ScreenUpdating = False
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
    
        On Error GoTo cleanup
        For Each cell In Range("G1:G20")
            strbody = strbody & cell.Value & vbNewLine
        Next
        For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
            If cell.Value Like "?*@?*.?*" And _
               LCase(Cells(cell.Row, "C").Value) = "yes" Then
                stemails = stemails & cell & ";"
            End If
        Next cell
    
        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .bcc = stemails
            .Subject = "Reminder"
            .Body = "Dear " & Cells(cell.Row, "A").Value & vbNewLine & vbNewLine & strbody
            'You can add files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display    'Or use Send
        End With
        On Error GoTo 0
        Set OutMail = Nothing
    
    
    cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
    End Sub
    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,814

    Re: Multiple email recipients from excel

    Try

    Code:
    Sub TestFile()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim cell As Range
        Dim strbody As String
    
        Application.ScreenUpdating = False
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
    
        On Error GoTo cleanup
        
        strbody = "This is the message"
        
        
        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
                
       For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
            If cell.Value Like "?*@?*.?*" And _
               LCase(Cells(cell.Row, "C").Value) = "yes" Then
                OutMail.BCC = OutMail.BCC & ";" & cell.Value
            End If
      Next cell
      
    With OutMail
        .Subject = "Reminder"
        .Body = "Dear everybody " & vbNewLine & vbNewLine & strbody
        'You can add files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display  'Or use Send
    End With
    On Error GoTo 0
    Set OutMail = Nothing
       
    
    cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
    End Sub
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: Multiple email recipients from excel

    Hi Richard,

    Thank you for your help and advice. That worked perfectly and apols for absence of code tags - now understand this.

    Kind regards

    Steve

  6. #6
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: Multiple email recipients from excel

    Hi Martin,

    Thanks to you also for a quick response. I really appreciate your willingness to look at this for me.

    Best wishes,

    Steve

  7. #7
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: Multiple email recipients from excel

    Hi,

    Please would someone let me know how to invoke the macro by the easiest method

    I'm thinking this is to have a cell in the worksheet which gets clicked

    Again, help is appreciated

    Steve

  8. #8
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Multiple email recipients from excel

    Hi Steve,

    Probably the easiest way is to add an object of some sort, an icon, picture, drawing object like a rectangle etc. and associate your TestFile macro with it. i.e. once you have the object on the sheet, right click it, choose 'Assign macro' and then select the TestFile macro.

    Now whenever you click the object the macro will run.

    You can of course also run it directly from the Menu, 'Developer > Macros if you're using 2007, or Tools > Macro > Macros with 2003.

    HTH
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  9. #9
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: Multiple email recipients from excel

    Hi Richard,

    This is very simple but very effective, thanks - as you can tell my Excel skills are not the best

    Please would you help further with the vba code from the start of this thread?

    1. How do I format the body content so that it is presented as two distinct columns

    2. How do I add to the body content, the content of an additional cell or cells?

    Hope I'm not asking too much

    Best wishes,

    Steve

  10. #10
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Multiple email recipients from excel

    Hi,

    What exactly is the Body content? Presumably the macro which contains "Dear Everybody" and "This is the message" is just an example. I'm assuming the real body is a paragraph of text in the workbook.

    If so can you upload the actual workbook you're using?

    Rgds
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  11. #11
    Forum Contributor
    Join Date
    01-23-2010
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007
    Posts
    117

    Re: Multiple email recipients from excel

    Hi Richard,

    Here it is

    Steve
    Attached Files Attached Files

  12. #12
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    5,826

    Re: Multiple email recipients from excel

    Hi Steve,

    Please see the attached. Is this something along the lines of what you want. The macro to run is the 'CreateOutlookMessage'

    Let me know
    Attached Files Attached Files
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0