Results 1 to 3 of 3
  1. #1
    baronqueefington is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    32

    sending multiple emails wth vba

    Good evening again,

    I have a piece of code that allows me to send multiple emails from a query or table, my only issue is that get an error when it comes across a record without an email address. This shouldn't be the norm as all records should have email addresses but I want to prepare for all eventualities.

    I have tried using not Is null (MyRS![Email Address]) then to continue else re loop, but I don't seem to be able to get it right.

    Any help would be great.
    Private Sub cmdmulti_email_Click()
    Dim MyDB As Database
    Dim MyRS As Recordset
    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookRecip As Outlook.Recipient
    Dim objOutlookAttach As Outlook.Attachment
    Dim TheAddress As String
    Set MyDB = CurrentDb
    Set MyRS = MyDB.OpenRecordset("qryLapse")




    MyRS.MoveFirst


    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")

    Do Until MyRS.EOF
    ' Create the e-mail message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    TheAddress = MyRS![Email Address]

    With objOutlookMsg
    ' Add the To recipients to the e-mail message.
    Set objOutlookRecip = .Recipients.Add(TheAddress)
    objOutlookRecip.Type = olTo

    ' Set the Subject, the Body
    .Subject = "subject"
    .Body = "test"

    ' Resolve the name of each Recipient.
    For Each objOutlookRecip In .Recipients
    objOutlookRecip.Resolve
    If Not objOutlookRecip.Resolve Then
    objOutlookMsg.Display
    End If
    Next
    .Display

    End With
    MyRS.MoveNext
    Loop



    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing
    End If
    End Sub

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you can add an if loop

    Code:
    if isnull(theaddress) then
        'do whatever you're going to do on a blank email
    else
        'do your current code
    endif

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    And/or select only records that have email address.

    Set MyRS = MyDB.OpenRecordset("SELECT * FROM qryLapse WHERE Not [Email Address] Is Null;")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Sending Emails from Access via Outlook
    By Terry Lawson in forum Programming
    Replies: 3
    Last Post: 11-14-2014, 10:03 AM
  2. Sending multiple emails
    By Sephaerius in forum Database Design
    Replies: 4
    Last Post: 08-08-2013, 11:55 AM
  3. Sending Emails and VBA coding
    By lucy1216 in forum Programming
    Replies: 4
    Last Post: 05-20-2013, 05:57 AM
  4. Sending data from form to multiple emails
    By GeorgeB in forum Access
    Replies: 11
    Last Post: 01-09-2012, 04:23 PM
  5. Sending multiple emails
    By Petefured in forum Programming
    Replies: 0
    Last Post: 05-24-2011, 03:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums