【问题标题】:PDF email from Access来自 Access 的 PDF 电子邮件
【发布时间】:2015-04-21 00:34:06
【问题描述】:

我正在尝试通过单击命令按钮通过电子邮件发送提交表单。我创建了代码来根据 4 个主键过滤表单。但是当我运行代码时,FleetID 部分在即时窗格中显示为空白。 FleetID 部分在组合框中提供。有人可以帮帮我吗?

谢谢

On Error GoTo errhandle
    Me.Filter = "CurrentDate= #" & Format(Me!CurrentDate, "yyyy\-mm\-dd") & "# and DiscoverTime= '" & Me!DiscoverTime & "' And TailNumber= '" & Me!TailNumber & "' And FleetID= '" & Me!FleetID & "'"
    Debug.Print Me.Filter
    Me.FilterOn = True
    DoCmd.SendObject acSendForm, "frmETIC",  acFormatPDF, "EMAIL", "", "", "Recovery Report", "Attached is the submitted Recovery Report"

exiterr:
    Exit Sub
errhandle:
    If Err.Number <> 2501 Then
        MsgBox ("Email cancelled!")
    End If
Resume exiterr

【问题讨论】:

  • 您可以为我们添加您的表定义吗?你的fleetID是数字还是字符串?如果数字删除文字字符串符号 '
  • FleetID 是一个字符串。即使某些值是数字,我也将它作为短文本放在我的表格中。一些记录是 757 或 767,而另一些是 A300 和 MD-10,所以我决定将整个字段设置为一个简短的文本列,以使其更容易。
  • 是的,这是有道理的。我会建议你两件事。 1> 使用 cbo_fleet_id、txt_tail_number 之类的前缀命名您的控件。 2> 使用像me.cbo_fleet_id.value这样的完整命名方法。 Access 允许控件和字段名称相同,有时会混淆
  • 我发现我确实有一些错误的东西,非常感谢!您知道是否可以将通过电子邮件发送的 pdf 制作成横向格式,因为它很宽而且看起来很奇怪?

标签: email ms-access vba


【解决方案1】:

除了我在评论部分的建议。我会亲自从该查询中创建一个查询和所需的报告。与带有额外控件的表单不同,报告为您提供了非常整洁和专业的外观。

  • 首先创建一个与表单数据源相同的查询
  • 根据该查询创建报告。使用您的徽标页脚和您希望打印边距的所有其他内容来设计您的报告。
  • 使用 where 条件生成报告并使用 docmd.sendobject

或者

  • 使用您的 where 条件生成隐藏的报告
  • 使用 docmd.outputTo 将报告保存为 PDF 文件
  • 创建新的 Outlook 电子邮件对象并附加 PDF 文件

这两种方式都有各自的优势。我个人使用第二个,因为它对我来说更容易自定义电子邮件内容/模板。

这是一个创建电子邮件的函数:

Function SEND_EMAIL_MESSAGE(mTo As String, mCC As String, mBC As String, mSubject As String, mBody As String, Optional useOwnSignature As Boolean = False, Optional DisplayMsg As Boolean = False, Optional isHTML As Boolean = False, Optional AttachmentPath = "") As Boolean
' Please check the reference for Microsoft Outlook 14.0 object library for outlook 2010.

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem
    Dim objOutlookAttach As Outlook.Attachment
    Dim mSignature As String

    On Error GoTo ERROR_EMAIL
    ' Create the Outlook session.
    Set objOutlook = New Outlook.Application

    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

    With objOutlookMsg
        ' Add the To recipient(s) to the message.
        .To = mTo
        .CC = mCC
        .BCC = mBC
        .Subject = mSubject

        If useOwnSignature Then .BodyFormat = olFormatHTML
        .Display

        If useOwnSignature Then
            If isHTML Then

                mSignature = .HTMLBody
                .HTMLBody = mBody & mSignature
            Else
                mSignature = .Body
                .Body = mBody & mSignature
            End If
        Else
            .Body = mBody
        End If

        ' Add attachments to the message.
        If Not IsMissing(AttachmentPath) Then
            Dim mFiles() As String
            If (VBA.Right(AttachmentPath, 1)) <> ";" Then AttachmentPath = AttachmentPath & ";"
            mFiles = VBA.Split(AttachmentPath, ";")
            Dim i As Integer
            For i = 0 To UBound(mFiles) - 1
                If Not mFiles(i) = "" Then Set objOutlookAttach = .Attachments.Add(mFiles(i))
            Next i

        End If

        ' Should we display the message before sending?
        If DisplayMsg Then
            .Display
        Else
            .Send
        End If
    End With

    SEND_EMAIL_MESSAGE = True
EXIT_ROUTINE:
    On Error GoTo 0
    Set objOutlook = Nothing
    Set objOutlookMsg = Nothing
    Exit Function

ERROR_EMAIL:
    SEND_EMAIL_MESSAGE = False
    GoTo EXIT_ROUTINE
End Function

这里有一些代码可以生成报告并发送到电子邮件:

strReportName = "rpt_incident_view_single"

        DoCmd.OpenReport strReportName, acViewPreview, , strCriteria, acHidden

        Dim tmpPath As String
        tmpPath = VBA.Environ("temp")
        strMyPath = tmpPath
        If VBA.Right(strMyPath, 1) = "\" Then
            strMyPath = strMyPath & "_" & incident_id & "_" & VBA.Format(Now, "yyyy-dd-mm") & ".pdf"
        Else
            strMyPath = strMyPath & "\" & "_" & incident_id & "_" & VBA.Format(Now, "dd-mm-yyyy") & ".pdf"
        End If

        DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strMyPath, False

保存报告后,只需将其发送到电子邮件功能,该功能将创建新电子邮件并将其显示给用户:

SEND_EMAIL_MESSAGE mTo, mCC, mBcc, mSubject, mBody,,,, strMyPath
DoCmd.Close acReport, strReportName
on error resume next
VBA.Kill strMyPath

只需根据您的需要修改您的代码。祝你好运:)

【讨论】:

  • 这是很棒的信息...但我尽量远离报告,因为我想单独查看每条记录。
  • 即使您可以通过报告来实现;)
  • 真的吗?!教我你的方法
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多