PDA

View Full Version : Emailing from Access to Outlook using Query and Mailto:



Wdarwin
08-19-2020, 03:14 AM
Hi Everyone

Im trying to output from an access query (or it can be a table but i would need to have a check against another active field in this scenario).. direct to the to: field in outlook.

I have this in place for a single field in a form using the Application.Followhyperlink function but this wont work in terms of pulling in a full list of email addresses.

So i have a Query called "AllEmailAddresses" where column 1 is "EmailAddress" and i want to have a button on a form with the click event to attach all the email addresses in that column into outlook in the to: field, semi colon in between obviously :)

I am very new to VB so getting this to work is proving a nightmare... can anyone else using there expertise?

OBP
08-19-2020, 03:52 AM
Here is an example of the code you need from one of my databases


Dim subject As String, Body As String, EmailAddress As String, sql As String
Dim rs As Object, recount As Integer, count As Integer, swhere As String
On Error GoTo errorcatch
If Me.Extra = -1 Then ' you may not need this as it is a test to see if extra people are included
sql = "SELECT emailExtraQuery.* " & _
"FROM emailExtraQuery " & _
"Where Xtra = -1" & _
"And email = -1"
Else
If Me.Year <> "All" Then
sql = "SELECT emailQuery.* " & _
"FROM emailQuery " & _
"Where RenewalYear = " & Me.Year & _
"And email = -1"
Else
sql = "SELECT emailall.* " & _
"FROM emailall " & _
"Where email = -1"
End If
End If
Set rs = CurrentDb.OpenRecordset(sql)
'MsgBox rs.RecordCount
If rs.RecordCount > 0 Then
rs.MoveLast
rs.MoveFirst
'MsgBox rs.RecordCount
recount = rs.RecordCount
For count = 1 To recount
If Not IsNull(rs![Home e-mail]) Then EmailAddress = EmailAddress & rs![Home e-mail] & ";"
If Not IsNull(rs![Work e-mail]) Then EmailAddress = EmailAddress & rs![Work e-mail] & ";"
rs.MoveNext
Next count
EmailAddress = Left(EmailAddress, Len(EmailAddress) - 1)
'MsgBox EmailAddress
DoCmd.SendObject , , , "if you want to send yourself a copy of the email put yor email address here", , EmailAddress, , , True
End If
rs.Close
Set rs = Nothing
Exit Sub
errorcatch:
MsgBox Err.Description

Note the true on the end of the docmd.sendobject shows you the email address before trying to send anything.
If you wish to compose the subject and body of the email prior to sending you can do so using a form to input them first as per the attached database.