将电子邮件地址传递给VBA中的SendObject方法

时间:2013-01-22 23:30:31

标签: email vba ms-access-2007

我想从我的表'GetContact_TempTbl'获取电子邮件地址并将报告发送到该电子邮件地址。此电子邮件将根据收到的公司更改。我目前提取相关的电子邮件地址并将其存储在临时表中我目前收到了Object Required错误。

非常感谢你的建议。

Dim db As Database
Dim rs As Recordset
Dim stRecipients As String
Dim stDocName As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
Set stRecipients = rs.Fields("Contact_Email")
stDocName = "License CODs"
stRecipietns = stRecipients

DoCmd.SendObject acReport, stDocName, acFormatPDF, stRecipients, , , "Thank You for your purchase"

2 个答案:

答案 0 :(得分:0)

您应该只将set与对象一起使用,而不是字符串:

Dim db As Database
Dim rs As Recordset
Dim stRecipients As String
Dim stDocName As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
''This is not a field object, it is a string
stRecipients = rs.Fields("Contact_Email")
stDocName = "License CODs"
stRecipietns = stRecipients

DoCmd.SendObject acReport, stDocName, acFormatPDF, _
   stRecipients, , , "Thank You for your purchase"

如果您说明如何创建临时表,则可以使这更容易。

答案 1 :(得分:0)

如果您的记录集为每个要发送电子邮件的收件人保留一行,请遍历记录集以收集它们,而不是仅从第一行读取收件人。

Const stDocName As String = "License CODs"
Dim db As DAO.database
Dim rs As DAO.Recordset
Dim stRecipients As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
With rs
    Do While Not .EOF
        stRecipients = stRecipients & ";" & !Contact_Email
        .MoveNext
    Loop
    .Close
End With

If Len(stRecipients) > 0 Then
    ' discard leading ";"
    stRecipients = Mid(stRecipients, 2)
    DoCmd.SendObject acReport, stDocName, acFormatPDF, _
        stRecipients, , , "Thank You for your purchase"
Else
    MsgBox "No recipients to email!"
End If

Set rs = Nothing
Set db = Nothing

但是,如果我的解释不正确,并且记录集总是包含只有一个Contact_Email的单行,则您甚至不需要记录集。您只需使用Contact_Email检索DLookup

stRecipients = Nz(DLookup("Contact_Email", "GetContact_TempTbl"), "")