使用不同的 pdf 附件将电子邮件发送到多个地址 [英] Sending e-mails to multiple addresses with different pdf attachments

查看:32
本文介绍了使用不同的 pdf 附件将电子邮件发送到多个地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有命令按钮的 Access 表单,可以打开报告并在本地文件夹中创建 .PDF 文件.每个 .PDF 报告都有不同的名称(1234.pdf、4321.pdf 等).数字代表员工编号,报告是该员工当前的休假时间余额.所以基本上,我最终在文件夹中得到了大约 60 个 .PDF 文件,每个文件都用于不同的员工.创建这些 .PDF 文件后,我希望通过 Access 通过电子邮件将它们连同自己的 .PDF 附件通过电子邮件发送给每个员工,而不是我在 Outlook 中创建单独的电子邮件并手动附加文件.我知道如何将一个 .PDF 文件附件发送给一个或多个收件人,但无法弄清楚如何将特定的 .PDF 文件附件发送给各个收件人.我在想,也许文件可以在创建后立即发送,然后继续创建下一个文件,等等.我不知道.下面是我用来为每个员工编号创建 .PDF 文件的代码.顺便说一句,如果有帮助的话,员工编号和电子邮件地址存储在同一个员工表中.

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

推荐答案

由于您已经拥有生成 .pdf 文件的代码,因此请使用以下代码将电子邮件发送到带有单独 pdf 的单个电子邮件地址.这里 EmpID 是员工编号字段,Email 是员工电子邮件地址字段.如果您的电子邮件字段名称不同,请进行调整.

As you already have codes to generate .pdffiles 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.

希望您了解 添加引用 Microsoft Outlook x.xx 对象库.

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

这篇关于使用不同的 pdf 附件将电子邮件发送到多个地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆