Email portions of a report to users as a pdf

AccessRube

Registered User.
Local time
Today, 02:11
Joined
May 21, 2012
Messages
13
Good morning.

I have an event planning tool I've developed that I'm trying to add some functionality to. I have a list of attendee's and their schedules in an aggregate query, and then formatted a report based off that query to make it look pretty ;). What I'd like to be able to do is send each attendee an email with only their specific section of the report as a pdf attachment. I have a module that already sends customized emails, but what I really could use is a hand with the code that would select only their portion of the report, create it as a pdf, and then attach it. If you have any experience doing something like this I'd greatly appreciate the help.

Many thanks
 
Thanks, I'll give those a read.
 
No problem; post back if you get stuck.
 
A lot of good stuff. I apologize though, I'm still a bit green. I have the report that has their schedules, and I understand that I need to put a procedure on the On Load event of that report. I'm assuming I'd want to use my Employee ID for that, without seeing the DB used in the examples I'm a little unclear on what the LoadID and ShipmentID's are referring to. I have another query that I believe should be my recordset, as that has the email addresses of the recipients, and should be used to decide who is receiving the emails in the first place. Am I mistaken in thinking I could put the report creation and attachment within the loop I currently have? I'm customizing a text file that is the body of the report. I'll still want to do that, only with the report attached. Hence the reason I'd like to make this code do both...


Current email module I'm using:
Code:
Public Function SendEMail()

Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Dim MynewBodyText As String

Set fso = New FileSystemObject

' First, we need to know the subject.

' We can??t very well be sending around blank messages...

Subjectline$ = InputBox$("Please enter the subject line for this mailing.", _
"We Need A Subject Line!")

' If there??s no subject, call it a day.

If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If

' Now we need to put something in our letter...

'BodyFile$ = InputBox$("Please enter the filename of the body of the message.", _
'"We Need A Body!") Commented to hardcode file for testing.

'Uncomment line below for non executive email template
BodyFile$ = "C:\Users\User1\Desktop\NonExecs.txt"

' If there??s nothing to say, call it a day.

If BodyFile$ = "" Then
MsgBox "No body, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain??t Got No-Body!"
Exit Function
End If

' Check to make sure the file exists...
If fso.FileExists(BodyFile$) = False Then
MsgBox "The body file isn??t where you say it is. " & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "I Ain??t Got No-Body!"
Exit Function
End If

' Since we got a file, we can open it up.
Set MyBody = fso.OpenTextFile(BodyFile, ForReading, False, TristateUseDefault)

' and read it into a variable.
MyBodyText = MyBody.ReadAll

' and close the file.
MyBody.Close

' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application

' Set up the database and query connections

Set db = CurrentDb()

Set MailList = db.OpenRecordset("MyEmailAddresses")

' now, this is the meat and potatoes.
' this is where we loop through our list of addresses,
' adding them to e-mails and sending them.

Do Until MailList.EOF

' This creates the e-mail

Set MyMail = MyOutlook.CreateItem(olMailItem)

' This addresses it

MyMail.To = MailList("email")

'This gives it a subject
MyMail.Subject = Subjectline$

MyMail.Importance = olImportanceHigh

' Now we can replace tokens to our heart's content
' without worrying about corrupting the "master" template

MynewBodyText = MyBodyText

MynewBodyText = Replace(MynewBodyText, "[[Display Name]]", MailList("DisplayName"))

MynewBodyText = Replace(MynewBodyText, "[[ArrivalDate]]", MailList("EstimatedArrivalDate"))

MynewBodyText = Replace(MynewBodyText, "[[DepartureDate]]", MailList("EstimatedDepartureDate"))

MynewBodyText = Replace(MynewBodyText, "[[ArrivalTime]]", MailList("Arrival Time"))

MynewBodyText = Replace(MynewBodyText, "[[DepartureTime]]", MailList("Departure Time"))

'MynewBodyText = Replace(MynewBodyText, "[[AdminName]]", MailList("AdminName"))

'This gives it the body
MyMail.Body = MynewBodyText

'If you want to send an attachment
'uncomment the following line

MyMail.Attachments.Add "C:\Users\User1\Desktop\EmployeeRegistrationLetter_5.2.12.docx", olByValue, 1, "Registration Instructions" 'The Displayname doesn't work since it isn't RTF

' To briefly describe:
' "c:myfile.txt" = the file you want to attach
'
' olByVaue = how to pass the file. olByValue attaches it, olByReference creates a shortcut.
' the shortcut only works if the file is available locally (via mapped or local drive)
'
' 1 = the position in the outlook message where to attachment goes. This is ignored by most
' other mailers, so you might want to ignore it too. Using 1 puts the attachment
' first in line.
'
' "My Displayname" = If you don??t want the attachment??s icon string to be "c:myfile.txt" you
' can use this property to change it to something useful, i.e. "4th Qtr Report"

'This sends it!

'MyMail.Send

'Some people have asked how to see the e-mail
'instead of automaticially sending it.
'Uncomment the next line
'And comment the "MyMail.Send" line above this.

MyMail.Display

'And on to the next one...
MailList.MoveNext

Loop

'Cleanup after ourselves

Set MyMail = Nothing

'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.

'MyOutlook.Quit
Set MyOutlook = Nothing

MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing

End Function

I guess my question is where in here should I put the code to create the report? And if you feel like really helping out, what does that code look like?

As you can imagine I didn't write this code myself. I understand what it's doing, but I'm still working on coming up with code from scratch myself.
 
As noted in the link, it would go in the On Open event of the report. In your loop, you'd have a line to set the form control to the current employee:

Forms!FormName.TextboxName = MailList!FieldName

a line to output the report:

DoCmd.OutputTo acOutputReport, "ReportName", acFormatPDF, "PathHere"

then attach the file to the email.
 

Users who are viewing this thread

Back
Top Bottom