combine multiple email addresses into .bcc from recordset to send outlook email from access vba (1 Viewer)

adhoustonj

Member
Local time
Today, 07:15
Joined
Sep 23, 2022
Messages
150
Hello,
I hope this post is in the right place. The code below is populating the bcc and to fields in my outlook message. Is it possible to only populate the bcc and leave the to field blank? Or am I approaching this wrong and should loop through and send an individual email to each email from the recordset?





Code:
Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OutMail As Object
    Dim strEmail As String
    Dim bccEmails As String


    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)


    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Email FROM tblEmail")
    With rs
        Do Until .EOF
            strEmail = !Email
            ' add email to BCC email list string
            bccEmails = bccEmails & strEmail & ";"


            With OutMail
                .Recipients.Add strEmail
            End With


            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing


    ' set BCC using string of concatenated emails                 
    OutMail.BCC = left(bccEmails, Len(bccEmails) - 1) ' trims trailing semicolon


    OutMail.Display


End Sub
 

adhoustonj

Member
Local time
Today, 07:15
Joined
Sep 23, 2022
Messages
150
Never mind... I see what was happening. the .recipients.Add strEmail was adding them to the .to and then outMail.BCC is being set with bccEmails
 

ebs17

Well-known member
Local time
Today, 13:15
Joined
Feb 7, 2020
Messages
1,946
Concat is shorter when using an ADODB.Recordset.

Code:
' set BCC using string of concatenated emails                 
 OutMail.BCC = ADODBRecordset.GetString(adClipString, , , ";")
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:15
Joined
Sep 21, 2011
Messages
14,304
Hello,
I hope this post is in the right place. The code below is populating the bcc and to fields in my outlook message. Is it possible to only populate the bcc and leave the to field blank? Or am I approaching this wrong and should loop through and send an individual email to each email from the recordset?





Code:
Private Sub Command180_Click()
    Dim rs As DAO.Recordset
    Dim OlApp As Object
    Dim OutMail As Object
    Dim strEmail As String
    Dim bccEmails As String


    Set OlApp = CreateObject("Outlook.Application")
    Set OutMail = OlApp.CreateItem(olMailItem)


    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT Email FROM tblEmail")
    With rs
        Do Until .EOF
            strEmail = !Email
            ' add email to BCC email list string
            bccEmails = bccEmails & strEmail & ";"


            With OutMail
                .Recipients.Add strEmail
            End With


            rs.MoveNext
        Loop
    End With
    rs.Close
    Set rs = Nothing


    ' set BCC using string of concatenated emails                
    OutMail.BCC = left(bccEmails, Len(bccEmails) - 1) ' trims trailing semicolon


    OutMail.Display


End Sub
You really should be checking for eof before trying to use a recordset?
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:15
Joined
Sep 21, 2011
Messages
14,304
I just check for EOF. If the recordset is empty when you open it, that is True as is BOF, however I just use the one test, EOF.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:15
Joined
Sep 21, 2011
Messages
14,304
Yes, but if you are looping, I would use Do Until rst.EOF
 

Users who are viewing this thread

Top Bottom