+ Reply to Thread
Results 1 to 11 of 11

Help Programming Email Via Excel Spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Help Programming Email Via Excel Spreadsheet

    Hi all,

    I am trying to create a macro from a sheet formatted in the following way: (the sheet is a list of products we do not have in stock)

    Name        Product        Email                       Send Email
    John         Part A          [email protected]       Yes
    Mike         Part B          [email protected]         Yes
    John         Part C          [email protected]       Yes
    What I want to happen when I run the macro is an email is created/sent for each UNIQUE email address with a list of the parts that we have been unable to deliver to them.

    So for the example above, the email would read:

    Dear John, 
    
    Unfortunately, we have been unable to supply the following products to you today
    
    Part A
    Part C
    
    Please call us on 01111111 if you would like to re-order when we fresh stock arrives, 
    
    Regards, 
    
    DEFAULT OUTLOOK SIGNATURE INCLUDED
    I have played around with the following, but am struggling with a way to tell excel to compile all shorts with the same email...

    Sub Test1()
        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 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
                    .To = cell.Value
                    .Subject = "Reminder"
                    .Body = "Dear " & Cells(cell.Row, "A").Value _
                          & vbNewLine & vbNewLine & _
                            "Unfortunately, we have been unable to supply the following products to you today " & _
                            vbNewLine & vbNewLine & _
                            Cells(cell.Row, "A").Value _
                            
                    'You can add files also like this
                    '.Attachments.Add ("C:\test.txt")
                    .Send  'Or use Display
                End With
                On Error GoTo 0
                Set OutMail = Nothing
            End If
        Next cell
    
    cleanup:
        Set OutApp = Nothing
        Application.ScreenUpdating = True
    End Sub
    Any help with this would be appreciated.

    And also what code would include the default signature from my outlook?

    Thanks in advance

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Help Programming Email Via Excel Spreadsheet

    I'd suggest that you sort and filter on the email address. Then you can send the visible cells using Ron de Bruin's approach ... http://www.rondebruin.nl/win/s1/outlook/bmail2.htm

    You can use an Advanced Filter to extract a list of unique email addresses and loop through that to Autofilter and send the data.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Help Programming Email Via Excel Spreadsheet

    Hi,

    Thanks for the reply. I'm not sure the above would address my problem?

    To clarify, the spreadsheet would be 50 lines+, so to use the filter function would take too much time really.

    I'm really looking for one Macro that would do all emails if possible?

  4. #4
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Help Programming Email Via Excel Spreadsheet

    Hi,

    No, I can't get that to work for me. My list may have 30 different emails on, with many of them repeated. I need the macro to create one mail per unique email address, but include the details from all rows associated with that email address...

    I apologise for my lack of coding knowledge....

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Help Programming Email Via Excel Spreadsheet

    Or use Dictionary to create unique list and email.
    Sub tst()
        Set dic = CreateObject("scripting.dictionary")
        With Sheets("Sheet1")
            sn = .Cells(1).CurrentRegion.Value
            For i = 2 To UBound(sn)
                x0 = dic.Item(sn(i, 3))
            Next
            For j = 0 To dic.Count - 1
                .Cells(1).CurrentRegion.AutoFilter 3, dic.keys()(j)
                .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1") 'temp sheet to create htmlRange
                'code to create email
                '....
                Sheets("Sheet2").Cells(1).CurrentRegion.ClearContents 'clear range for next email
            Next
        End With
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  6. #6
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Help Programming Email Via Excel Spreadsheet

    Quote Originally Posted by bakerman2 View Post
    Or use Dictionary to create unique list and email.
    Sub tst()
        Set dic = CreateObject("scripting.dictionary")
        With Sheets("Sheet1")
            sn = .Cells(1).CurrentRegion.Value
            For i = 2 To UBound(sn)
                x0 = dic.Item(sn(i, 3))
            Next
            For j = 0 To dic.Count - 1
                .Cells(1).CurrentRegion.AutoFilter 3, dic.keys()(j)
                .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet2").Range("A1") 'temp sheet to create htmlRange
                'code to create email
                '....
                Sheets("Sheet2").Cells(1).CurrentRegion.ClearContents 'clear range for next email
            Next
        End With
    End Sub
    I'm just playing with this one now.....

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Help Programming Email Via Excel Spreadsheet

    You can try out the attachment and let us know how this works for you.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Help Programming Email Via Excel Spreadsheet

    Hi Bakerman,

    The Macro runs, but no emails are generated?

    Am I doing something wrong here (been a loong day!)

  9. #9
    Registered User
    Join Date
    09-08-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    70

    Re: Help Programming Email Via Excel Spreadsheet

    Sorry, to correct myself there...

    It's almost there. Just 2 small points.

    1. The macro creates the mails, but how do I get them to send rather than just create them.
    2. How would I go about adding my default signature to the emails?

    Many thanks for your patience!! It really is appreciated!

  10. #10
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Help Programming Email Via Excel Spreadsheet

    I can answer the first not sure how to get the sig to work.....

    find the line that has

    .display '.Send
    and remove the .display ' and leave the .Send
    Bramz

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,630

    Re: Help Programming Email Via Excel Spreadsheet


+ 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: 2
    Last Post: 08-25-2014, 03:37 PM
  2. [SOLVED] Excel programming that sends an email to different recipients
    By joshnvince in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-26-2014, 05:40 PM
  3. Excel Automatic Email - VBA/ Macros/ Programming
    By workallday in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-08-2013, 07:55 PM
  4. Automatic email send
    By mmanalang in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2012, 03:51 AM
  5. Excel code to email whats in the spreadsheet in the body of email
    By Kain93 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2012, 09:55 AM
  6. excel spreadsheet to email
    By dougvcd in forum Excel General
    Replies: 1
    Last Post: 08-31-2009, 02:28 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