
Sending reports as attachments through Outlook
I pulled the following code off the MS site and wonder if I can get some
help (the purpose is to use automation to send some Access reports through
Outlook). I have three specific questions: (1) how can I get the recipitent
line to roll through the record set on which the report is based: is there
some SQL/VBA I can use [I would like to attach one record-specific page to a
message, have it sent, then attach another page and so on]? I thought of
setting up an array or a loop . . . . (2) What's the AttachmentPath
referred to in section under .Body? (3) Has anyone tried this and will it
work or is there abetter way?
Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Nancy Davolio")
objOutlookRecip.Type = olTo
' Set the Subject, Body, and Importance of the message.
.Subject = "Dining Room Invoice"
.Body = "You will find attached your most recent invoice for
meals taken in the Dining Room as of the date indicated. " &vbCrLf &vbCrLf
&"Please remit payment with a copy of the invoice to the following address
as soon as possible:" &vbCrLf &vbCrLf &"Michael Ratzlaff" &vbCrLf &vbCrLf
' Add attachments to the message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
' Resolve each Recipient's name.
For Each ObjOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Sub