+ Reply to Thread
Results 1 to 10 of 10

Automated e-mail from macro button in Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2003
    Location
    Decatur, Alabama
    Posts
    94

    Automated e-mail from macro button in Excel

    Dear Friends,

    I am wanting to automate the sending of e-mails to employees. The spreadsheet includes three columns, and multiple rows (no header) - each row is for one employee. The first column contains the name of the individual, the 2nd the e-mail address to use, the 3rd a bit of information that I need to include in the message area of the e-mail along with some of my own text (that will be exactly the same in every e-mail.)

    Here is an example

    Column 1 Column 2 Column 3
    Joe [email protected] $23.45
    Sally [email protected] $55.43

    The e-mails would be like this:

    Dear Joe, I wanted to let you know that you will receive $23.45 today.

    Dear Sally, I wanted to let you know that you will receive $55.43 today.


    Any help you can give would be most appreciated!
    Mike
    Learn to Serve Others. Kindness is far better than the alternative.

  2. #2
    Forum Contributor
    Join Date
    09-02-2005
    Posts
    146
    I go to this website when I'm trying to do e-mail related tasks.

    http://www.rondebruin.nl/sendmail.htm

    It should have about everything you need.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello mjwillyone,

    Here is macro customized to your needs. The name is column "A", the emial address in column "B", and the amount in column "C". The subject line includes the salutation and the amount. You can make changes to to starting row and the columns if you need to. I have highlighted them in red.
    Private Declare Function ShellExecute _
      Lib "Shell32.dll" _
        Alias "ShellExecuteA" _
          (ByVal hWnd As Long, _
           ByVal lpOperation As String, _
           ByVal lpFile As String, _
           ByVal lpParameters As String, _
           ByVal lpDirectory As String, _
           ByVal nShowCmd As Long) As Long
    
    Sub SendEmails()
    
      Dim Msg As String
      Dim R As Long
      Dim RetVal As Long
      Dim Subj As String
      Dim URL As String
    
          StartRow = 1
          LastRow = Cells(Rows.Count, "A").End(xlUp).Row
          LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
          
          For R = StartRow To LastRow
            Subj = "Dear " & Cells(R, "A").Text & ", I wanted to let you know that you will receive " & Cells(R, "C").Text
            URL = "MailTo:" & Cells(R, "B").Text & "?subject=" & Subj
            RetVal = ShellExecute(0&, "open", URL, vbNullString, vbNullStirng, 0&)
             'Did Connection Fail? Errors are from 0 to 32
              If RetVal <= 32 Then
                Select Case RetVal
                 Case 2     'SE_ERR_FNF
                   Msg = "File not found"
                 Case 3      'SE_ERR_PNF
                   Msg = "Path not found"
                 Case 5      'SE_ERR_ACCESSDENIED
                   Msg = "Access denied"
                 Case 8      'SE_ERR_OOM
                   Msg = "Out of memory"
                 Case 32     'SE_ERR_DLLNOTFOUND
                   Msg = "DLL not found"
                 Case 26     'SE_ERR_SHARE
                   Msg = "A sharing violation occurred"
                 Case 27     'SE_ERR_ASSOCINCOMPLETE
                   Msg = "Incomplete or invalid file association"
                 Case 28     'SE_ERR_DDETIMEOUT
                   Msg = "DDE Time out"
                 Case 29     'SE_ERR_DDEFAIL
                   Msg = "DDE transaction failed"
                 Case 30     'SE_ERR_DDEBUSY
                   Msg = "DDE busy"
                 Case 31     'SE_ERR_NOASSOC
                   Msg = "Default Email not configured"
                 Case 11     'ERROR_BAD_FORMAT
                   Msg = "Invalid EXE file or error in EXE image"
                 Case Else
                   Msg = "Unknown error"
                End Select
                Msg = "Unable to Send Email to " & vbCrLf & "'" & MailTo & "'" & vbCrLf _
                    & vbCrLf & "Error Number " & CStr(RetVal) & vbCrLf _
                    & Msg
                RetVal = MsgBox(Msg, vbExclamation + vbOKOnly)
              End If
          Next R
          
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    03-06-2013
    Location
    australia
    MS-Off Ver
    excel 2010
    Posts
    1

    Talking Re: Automated e-mail from macro button in Excel

    thats great - thanks for this. its a real gem of a find....

  5. #5
    Registered User
    Join Date
    10-15-2013
    Location
    Unna, Germany
    MS-Off Ver
    Excel 2013
    Posts
    1

    Re: Automated e-mail from macro button in Excel

    Hi,

    how to add a text?

    this only adds a subject but not the main message... does it?

    Best Regards

    Roy

  6. #6
    Registered User
    Join Date
    04-29-2015
    Location
    ireland
    MS-Off Ver
    2010
    Posts
    2

    Re: Automated e-mail from macro button in Excel

    Hi Leith Ross
    This macro is great, thanks so much, I am just wondering 2 further things.
    1. When I Run the macro, the email generates in Outlook as expected but do i have to hit the 'Send' button each time manually? I have a very large list of emails that I will be generating, is there any way to add function to auto send the mail as well as soon as you run the macro?
    2. I am trying to add in the email some text, instead of just have the info on the subject line, but I can't seem to get it to work. I have tried adding 'Body= ' etc and variations of this but the macro throws up an error each time. Ideally I want to add in a line of text and then also if possible pull a table/data directly from the excel worksheet and show it in the body of the mail.

    Any help would be great from anyone with some suggestion to add to the copied macro above.
    Thanks
    Maccabarra:

  7. #7
    Registered User
    Join Date
    03-14-2013
    Location
    Central West, NSW
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Automated e-mail from macro button in Excel

    Insert "PtrSafe" between "Declare" and "Function" in the first line for 64-bit systems...

  8. #8
    Registered User
    Join Date
    05-01-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Automated e-mail from macro button in Excel

    doesnt work on excel on mac. can you please help

    thanks

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Automated e-mail from macro button in Excel

    vinay,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  10. #10
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,951

    Re: Automated e-mail from macro button in Excel

    maccabarra, welcome to the forum

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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