0
我正嘗試使用以下代碼將附件發送到電子郵件列表。成功發送的電子郵件,但第二個電子郵件地址將獲得附件和第一個電子郵件地址的附件,第三個電子郵件地址的附件將獲得第一個和第二個附件。清除下一個循環的電子郵件附件
我的問題是:有沒有什麼辦法可以在附件實際添加之前清除附件?我試圖搜索,但沒有運氣。
請指導我,謝謝。
Set rsInvoices = db.OpenRecordset("SELECT * FROM Invoices WHERE InvMonth = " & Month(dtPeriod) & " AND InvYear = " & Year(dtPeriod))
selectQuery = "SELECT Email FROM Student WHERE ID = "
On Error GoTo CloseReportHandler
If Not (rsInvoices.EOF And rsInvoices.BOF) Then
Do Until rsInvoices.EOF = True
DoCmd.OpenReport strReportName, acViewPreview, , "Invoice.SID = " & Chr(34) & rsInvoices!SID & Chr(34) & " AND Invoice.InvYear = " & rsInvoices!InvYear & " AND Invoice.InvMonth = " & rsInvoices!InvMonth
fileName = "Invoice-" & rsInvoices!SID & "-" & rsInvoices!InvYear & "-" & rsInvoices!InvMonth & ".pdf"
selectQuery = selectQuery + rsInvoices!SID
Set rsStudents = db.OpenRecordset("SELECT Email FROM Student WHERE ID = " & Chr(34) & rsInvoices!SID & Chr(34))
studEmail = rsStudents!Email
DoCmd.OutputTo acOutputReport, , acFormatPDF, path + fileName, False
DoCmd.Close acReport, "Invoice"
With cdomsg
.To = studEmail
.From = "[email protected]"
.subject = "Test Email"
.TextBody = "Hello"
.AddAttachment path + fileName
Set .Configuration = cdoconf
.Send
End With
MoveNextInvoice:
rsInvoices.MoveNext
Loop
Set cdomsg = Nothing
End If
CloseReportHandler:
Select Case Err
Case 2501
'MsgBox ("here")
Resume MoveNextInvoice
Case Else
MsgBox (Err.Description)
End Select
Set cdomsg = Nothing
'Kill (path + "*.pdf")
rsInvoices.Close
End Sub
我嘗試了你的第一個建議,它的工作原理!謝謝Gord! – user3227412