在 Access 中按 vba 发送附件

Send attachment by vba in Access

我有发送电子邮件的代码,但我需要在创建后发送访问报告,我有发送电子邮件的代码:

 User = Environ$("username")
    emailDC = "email@email.com"

    Dim mess_body As String
        Dim appOutLook As Object
        Dim MailOutLook As Object
        Set appOutLook = CreateObject("Outlook.Application")
        Set MailOutLook = appOutLook.CreateItem(olMailItem)

            Set appOutLook = CreateObject("Outlook.Application")
            Set MailOutLook = appOutLook.CreateItem(olMailItem)
            With MailOutLook

            .To = emailDC
            .Subject = User
            .HTMLBody = "Try access to file"

            .DeleteAfterSubmit = True   'This would let Outlook send th note without storing it in your sent bin
            .Send
            End With
            'MsgBox MailOutLook.Body
            Exit Sub
email_error:
            MsgBox "An error was encountered." & vbCrLf & "The error message is: " & Err.Description
            Resume Error_out
Error_out:

但我需要将其与 .pdf 格式的报告一起发送

将报告另存为 .pdf

使用DoCmd.OutputTo方法生成pdf:

DoCmd.OutputTo _
    acOutputReport, "Your Report Name", _
    acFormatPDF, "A:\Temporary\Place\To\Store\Report.pdf", _
    False,"", 0, acExportQualityPrint

MailItem 有一个 Attachments 属性 允许您 Add 附件 :

With MailOutLook
    '...
    .Attachments.Add(yourReportPath)
    '...
End With