2013-01-22 130 views
3

我想從我的表'GetContact_TempTbl'中獲取電子郵件地址並將報告發送到該電子郵件地址此電子郵件將根據收到的公司進行更改。相關的電子郵件地址,並將電子郵件地址傳遞給VBA中的SendObject方法

非常感謝存儲在臨時表中。我目前得到的必選對象的錯誤。在advace的意見。

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" 

回答

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,你甚至不需要一個記錄。您可以簡單地用DLookup檢索Contact_Email

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

非常感謝你,這工作像一個魅力。 – user2002083

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" 

如果您說出如何創建臨時表,可能會更容易。

相關問題