问题
I have an Access form with a command button that opens a report and creates .PDF files in a local folder. Each .PDF report has a different name (1234.pdf, 4321.pdf, etc.) The number represents the employee number and the report is that employee's current leave time balance. So basically, I end up with about 60 .PDF files in the folder, each one for a different employee. After creating these .PDF files, I'd like them to be e-mailed to each respective employee with their own .PDF attachment via Access instead of me creating a separate e-mail in Outlook and attaching the file manually. I know how to send one .PDF file attachment to one or more recipients but can't figure out how to send a particular .PDF file attachment individual recipients. I was thinking that perhaps the file could be sent right after it is created and then move on to create the next file, etc. I don't know. Below is the code I'm using to create the .PDF files for each employee number. BTW, the employee number and e-mail address are stored in the same employee table, if that helps.
Private Sub CmdAllLeavePDF_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
Dim sNow As String
mypath = "C:\Users\rbryan_2\Desktop\EDM Reports PDF\"
sNow = Format(Now(), "mmddyyyy")
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT DISTINCT [TblNames.EmpID] FROM [QurEmpLeaveCurrAll]", dbOpenSnapshot)
Do While Not rs.EOF
temp = rs("TblNames.EmpID")
MyFileName = rs("TblNames.EmpID") & " - " & sNow & ".PDF"
DoCmd.OpenReport "RptEmpLeaveCurrAll", acViewPreview, , "[TblNames.EmpID]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "RptEmpLeaveCurrAll", acSaveYes
DoEvents
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
Beep
Eval ("MsgBox ('PDF FILES CREATED!@Individual Employee Leave Totals Reports Were Successfully Created@In The EDM Reports PDF File Folder.@@',64,' Employee Data Management')")
Me!CmdN.SetFocus
End Sub
回答1:
As you already have codes to generate .pdf
files so use below codes to send emails to individual email address with separate pdf. Here EmpID
is employee number field and Email
is the field of employee email addresses. If you email field is different name then adjust it.
I hope you are aware about
Add References
Microsoft Outlook x.xx Object Library.
Option Compare Database
Option Explicit
Private Sub cmdSendMails_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim strEmail As String, strAttachment As String
Dim mypath As String
mypath = "C:\Users\rbryan_2\Desktop\EDM Reports PDF\"
'mypath = "C:\Users\Harun.Rashid\Desktop\My PDFs\"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT DISTINCT EmpID, EmpEmail FROM TblNames", dbOpenSnapshot)
On Error Resume Next 'Suppress errors
Do While Not rs.EOF
strAttachment = mypath & rs!EmpID & ".pdf" 'Pdf name exactly as employee ID.
strEmail = rs!EmpEmail 'Email address from table column.
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.Recipients.Add strEmail 'Add email address
.Subject = "Your subject text here."
.Body = "Your body text here."
.Attachments.Add strAttachment 'Attach PDF file.
'.Send
.Display 'Use .send to send the mail. Display will show the email editor window.
End With
Set oEmail = Nothing
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
来源:https://stackoverflow.com/questions/63680446/sending-e-mails-to-multiple-addresses-with-different-pdf-attachments