除了我在评论部分的建议。我会亲自从该查询中创建一个查询和所需的报告。与带有额外控件的表单不同,报告为您提供了非常整洁和专业的外观。
- 首先创建一个与表单数据源相同的查询
- 根据该查询创建报告。使用您的徽标页脚和您希望打印边距的所有其他内容来设计您的报告。
- 使用 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
只需根据您的需要修改您的代码。祝你好运:)