Ok then. This is a long one:
'The calling function will be different depending on your application
'It will look something like this
'Place this in a standard module and modify it for your application.
You may want to add a parameter for the report.
public function EmailReportHelper()
'Setup
Dim rst as new adodb.recordset
Dim strSql as string
strSql = "select vendor, email " _
& "from vendorContact "
'Fetch
rst.open strsql, currentproject.connection, adopenforwardonly,
adlockreadonly
'Iterate through vendors - call the email function for each separate
criteria desired
'The "DoEvents" may not be necessary - I like having it for good
measure.
while not rst.eof
emailreport "Your Report Name", "Vendor = '" & rst(0) & "'", rst(1)
doevents
rst.movenext
wend
'Cleanup
if not rst is nothing then
if rst.state = adstateopen then rst.close
set rst = nothing
end if
end function
'Use with a report that has a filter-examining output criteria (see
later)
'Place this in a standard module. You will probably need to add a
reference to the
'Outlook Object Library. You can also modify this code to use late
binding and get around having to
'set the reference.
Public Function emailReport(strReportName As String, _
strFilter As String, _
Optional strTo As String, _
Optional strSubject As String) As Boolean
On Error GoTo emailReport_Err
'Declarations
Dim objOutlook As Outlook.Application, objOutlookMsg As
Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient, objOutlookAttach As
Outlook.Attachment
'Delete old outputfile
deleteEmailFile
'Open report - must be one that examines the Email = Email filter to
output to the getEmailFile() path
'Why do I do this? Seems strange to express TRUE as 'Email' = 'Email'
- this allows me to use the same report
'in multiple areas. In my application, the user selects the type of
output (preview, print, email, fax, etc.)
DoCmd.OpenReport strReportName, acViewPreview, , "'Email' = 'Email'" &
iif(strFilter <> "", " and ", "") & strFilter
DoCmd.Close acReport, strReportName, False
'Email file has been created, now create the outlook file
' Create the Outlook session and message
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipients to the e-mail message.
If strTo <> vbNullString Then
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo
End If
' Set the Subject, the Body, and the Importance of the e-mail
message.
.Subject = strSubject
.Body = "See Attached."
.Importance = olImportanceNormal
'Attach the report to the e-mail message.
Set objOutlookAttach = .Attachments.Add(getEmailFile())
' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
'Display the message
.Display
End With
emailReport = True
emailReport_Exit:
On Error Resume Next
deleteEmailFile
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Exit Function
emailReport_Err:
emailReport = False
Select Case Err.number
Case -2079129595 'Insufficient privilage - Outlook is not open
MsgBox "You must have Outlook open to send this email.",
vbExclamation, "Outlook Is Not Open"
Resume emailReport_Exit
Case 287 'User canceled send
Resume emailReport_Exit
Case Else
logError Err.number, "emailReport", "Report: " &
strReportName & " Filter: " & strFilter
MsgBox "Unhandled error #" & Err.number & vbCrLf &
Err.Description, vbCritical, "ERROR"
Resume emailReport_Exit
End Select
End Function
'This goes in the report module.
'If opening with the view set as email, the filter will
'contain True as expressed by 'Email' = 'Email'. This is an indicator
'that the report must be exported as a snapshot file to the
'getEmailFile() location. The static variable ensures no duplicates
when pages are reformatted.
Private Sub Report_Page()
If Me.Filter Like "*'Email' = 'Email'*" Then
Static PRINTED As Boolean
If PRINTED = False Then
PRINTED = True
DoCmd.OutputTo acOutputReport, , acFormatSNP, getEmailFile(),
False
End If
End If
End Sub
'These last two helper functions are useful, but not required. I
always export email files to the same location.
Public Function getEmailFile() As String
getEmailFile = CurrentProject.Path & "\EmailOutputFile.snp"
End Function
Public Sub deleteEmailFile()
On Error Resume Next
If Dir(getEmailFile()) <> vbNullString Then Kill getEmailFile()
End Sub
Have fun with that one!
-Kris