+ Reply to Thread
Results 1 to 3 of 3

Send multiple e-mails from Excel using VBA

  1. #1
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Send multiple e-mails from Excel using VBA

    I am trying to modify this code to send variations of the msg, but it seems it is either/or and won't recognize the If-Then-Else statement I put in. My data has Names, Report Name, Due Dates. If the report is not overdue (Days > 0) I want to send one message and if it is overdue (Days <0) I want to send a difference email. I appreciate any assistance.

    Option Explicit

    Sub SendEmail()
    'Uses late binding
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim Subj As String
    Dim EmailAddr As String
    Dim Recipient As String
    Dim Msg As String
    Dim Document As String
    Dim Days As String

    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")


    'Loop through the rows
    For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "*@*" Then
    'Get the data
    Subj = "Compliance Reminder"
    Recipient = cell.Offset(0, -3).Value
    EmailAddr = cell.Value
    Document = cell.Offset(0, -1).Value
    Days = cell.Offset(0, 2).Value

    'Compose message
    If Days > 0 Then
    Msg = Recipient & vbCrLf & vbCrLf
    Msg = Msg & "This is a reminder that the "
    Msg = Msg & Document
    Msg = Msg & " is overdue due in "
    Msg = Msg & Days
    Msg = Msg & " days."
    Else
    Msg = Recipient & vbCrLf & vbCrLf
    Msg = "Holy Crap, you did it!"

    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0) 'olMailItem
    With MItem
    .to = EmailAddr
    .Subject = Subj
    .Body = Msg
    '.Send
    .Save 'to Drafts folder
    End With
    End If
    End If

    Next
    Set OutlookApp = Nothing
    End Sub
    Please Login or Register  to view this content.
    [/CODE]

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Send multiple e-mails from Excel using VBA

    I haven't tested this, but I think the first End If is in the wrong place. Move it immediately below the Msg = "Holy ..." line. I hope you are indenting the code structures to make it easier to spot this kind of error.

  3. #3
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Send multiple e-mails from Excel using VBA

    Works like a charm now. Thank you so much!

+ Reply to Thread

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.6.0 RC 1