Sending e-mail through Access

JMH022

Active Member
Joined
Mar 7, 2002
Messages
320
Hello all!

I have a database that I set up to send reports to specific e-mail recipients using this code:

‘Code for Microsoft Access – Send e-mail
DoCmd.SendObject acReport, "Report1", _
"RichTextFormat(*.rtf)", “recipient@recipientsemail.com”, "", "", Date & _
" Report”, _
"Attached is today’s report.", False, ""

I am curious to know if it is possible that when I send the reports, the e-mail is not saved to my Outlook 2000 ‘Sent Items’ folder. I have done this in Excel using code that I received from some helpful wizards on this board:

‘ Code for Microsoft Excel – Send e-mail – Delete from ‘Sent Items’ after sending
Sub SetRecipients()
Dim olApp As Object, olMail As Object
Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
Set rngeAddresses = ActiveSheet.Range("Y3:Y7")
For Each rngeCell In rngeAddresses.Cells
strRecipients = strRecipients & ";" & rngeCell.Value
Next
'format e-mail
olMail.To = strRecipients
olMail.Attachments.Add ActiveWorkbook.FullName
olMail.Subject = Date & " Report"
olMail.Body = "Attached is today’s report"
olMail.DeleteAfterSubmit = True
olMail.Send
End Sub

Is there a way to adapt this code so it will work in Access?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
The "Excel" code should work practically as-is with the exception that your routines for applying values to the methods just needs to be updated to match access.

There's an error in this line:
'strRecipients = strRecipients & ";" & rngeCell.Value
It will place a leading ; character ahead of all entries. Outlook probably forgives you for this and ignores it.

I've commented out the original lines and inserted new lines.
Make the SQL command (strSQL) more specific to match the data in your actual table.

Mike

Code:
Sub SetRecipients() 
Dim olApp As Object, olMail As Object 
'Dim rngeAddresses As Range, rngeCell As Range, strRecipients As String 
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL as String

Set dbs = CurrentDb()

strSQL = "SELECT * FROM tblName"

Set olApp = CreateObject("Outlook.Application") 
Set olMail = olApp.CreateItem(olMailItem) 

'Set rngeAddresses = ActiveSheet.Range("Y3:Y7") 

Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)

With rs
  Do Until rs.EOF
    strRecipients = strRecipients & .Fields(0).Value & ";" 
    .MoveNext
  Loop
  strRecipients = Left(strRecipients,len(strRecipients)-1)
End With

'For Each rngeCell In rngeAddresses.Cells 
'strRecipients = strRecipients & ";" & rngeCell.Value 
'Next 

'format e-mail 
olMail.To = strRecipients 
olMail.Attachments.Add ReportName
olMail.Subject = Date & " Report" 
olMail.Body = "Attached is today’s report" 
olMail.DeleteAfterSubmit = True 
olMail.Send 
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,310
Members
451,696
Latest member
Senthil Murugan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top