【问题标题】:Clear Email Attachment for the next loop为下一个循环清除电子邮件附件
【发布时间】:2014-03-29 06:11:38
【问题描述】:

我正在尝试使用以下代码将附件发送到电子邮件列表。电子邮件成功发送,但第二个电子邮件地址将获得附件以及第一个电子邮件地址的附件,因此第三个电子邮件地址将获得第一个和第二个电子邮件地址的附件。

我的问题是:有没有办法在附件实际添加之前清除附件?我试图搜索但没有运气。

请指导我,谢谢。

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 = "xxx@example.com"
            .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

【问题讨论】:

    标签: vba email ms-access email-attachments


    【解决方案1】:

    你可以做以下两件事之一:

    1. 在.AddAttachment 语句之前放置一个.Attachments.DeleteAll 语句,或者

    2. 创建 CDO.Message 对象 (Set cdomsg = ...),将其发送,Set cdomessage = Nothing 都在 Do Until 循环内,因此每次迭代都使用新的 CDO.Message 对象。

    【讨论】:

    • 我尝试了您的第一个建议,它有效!谢谢上帝!
    猜你喜欢
    • 1970-01-01
    • 2018-06-27
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多