访问查询到电子邮件

时间:2015-06-09 12:54:45

标签: database windows vba ms-access email-integration

我已经获得了附加查询,该查询显示了几行(有更多数据)但只有2个电子邮件地址。我想将相应的行发送到他们的相关电子邮件地址。所有这些细节每周都会改变,并且会有更多(有时更少)的电子邮件地址,因此我需要保留来自此表单的数据(而不是静态地址)。我已尝试制作报告,并可以设法将其发送到电子邮件地址,但我收到了此报告的多个表单/电子邮件。查询将显示大量记录,因此这不是一个可接受的解决方案。

感谢任何帮助。

编辑 - 我已经附上了显示数据的表格。我只想以任何可能的方式向每家公司发送他们自己的几行。请帮助:)

Table Layout

感谢。

Querydetails

我的完整代码:

Dim rS As DAO.Recordset
Dim dbS As DAO.Database
Dim Filepath As String
Dim Folderpath As String
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

Set dbS = CurrentDb()
Set rS = dbS.OpenRecordset("Select DISTINCT email, [CompName], [MainContact] FROM q66NonConfirmedTimesheets")
Do While Not rS.EOF

myemail = rS!email
mycompname = rS!CompName
myMainContact = rS!MainContact

'My Email the report
Folderpath = "C:\Reports\"
Folderpath = Folderpath & "WeeklyTimesheet, " & [mycompname] & " - " & Format(date, "dd mmm yyyy") & ".pdf"

DoCmd.OpenReport "Rpt01UnconfirmedTimesheets", acViewPreview, , "email = '" & myemail & "'"
    DoCmd.OutputTo acOutputReport, , "PDFFormat(*.pdf)", Folderpath, False
    DoCmd.Close acReport, "Rpt01UnconfirmedTimesheets"

If oOutlook Is Nothing Then
    Set oOutlook = New Outlook.Application
End If
Set oEmailItem = oOutlook.CreateItem(olMailItem)

With oEmailItem
.to = [myemail]
.Subject = "Unconfirmed Timesheets"
.Body = "Automatic email from my database"
.Attachments.Add Folderpath
.Display
End With
'End of my emailing report

rS.MoveNext
Loop
Set oEmailItem = Nothing
Set oOutlook = Nothing
Set rS = Nothing
Set dbS = Nothing

End Sub

我已加入我的" Onload"报告中的事件:

Me.Filter = "[email]='" & myemail & "'"

2 个答案:

答案 0 :(得分:1)

您需要在VBA中打开包含唯一电子邮件地址的记录集。 循环记录集,打开报告,过滤电子邮件地址并通过电子邮件发送报告。航空代码:

Dim rS as DAO.Recordset
Dim dbS as DAO.Database
Set dbS = CurrentDb()
Set rS = dbS.OpenRecordset("Select DISTINCT emailaddress FROM <yourquery>",DbOpenSnapshot)
Do While Not rS.EOF
<sometempvar> = rS!emailaddress
'your code to email filtered report using <sometempvar>'
rS.MoveNext
Loop
Set rS = Nothing
Set dbS = Nothing

答案 1 :(得分:0)

您需要创建一个查询以仅返回电子邮件指定查询以仅返回唯一值,在查询编辑器中有一个选项:

enter image description here