How to Attach a PDF Stored in Database Table to an Email

3link

Registered User.
Local time
Today, 00:06
Joined
Jun 1, 2012
Messages
12
I'm trying to write a code that automatically composes an outlook email message (but doesn't send on its own). I need the code to attach the PDF that is attached to the current record. Specifically, all of the records on my table have PDF attachments. I need to send these attachments with the automatically generated email. How do I attach documents attached to records in my database to an email message?

I already know how to generate an email with sendobject. However, I'm told sendobject doesn't support what I'm trying to do. Any suggestions?
 
GREETING....

Dim Msg As Outlook.MailItem
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
Set Msg = Item
Else
Exit Sub
End If

Set MsgAttachments = Msg.Attachments
MsgAttachments.Add rdSet(<fieldname>).value

'add recipients etc
End Sub

Function IsMail(ByVal itm As Object) As Boolean
IsMail = (TypeName(itm) = "MailItem")
End Function
 
GREETING....

Dim Msg As Outlook.MailItem
Dim MsgAttachments As Outlook.Attachments

If IsMail(Item) Then
Set Msg = Item
Else
Exit Sub
End If

Set MsgAttachments = Msg.Attachments
MsgAttachments.Add rdSet(<fieldname>).value

'add recipients etc
End Sub

Function IsMail(ByVal itm As Object) As Boolean
IsMail = (TypeName(itm) = "MailItem")
End Function
Thank you. But I'm getting a "Sub or Function not defined" error for "rdSet".
 
Thank you. But I'm getting a "Sub or Function not defined" error for "rdSet".

I revised the code a bit to define rdSet as DAO.Recordset and set it as Me.recordset. Now I'm getting an object required message for

Code:
If IsMail(Item) then
 
Use this. Replace the names and subject and attahcment etc with your rdSet(<field>) values

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
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Michael Suyama")
objOutlookRecip.Type = olCC
' Add the BCC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add("Andrew Fuller")
objOutlookRecip.Type = olBCC
' Set the Subject, Body, and Importance of the message.
.Subject = "This is an Automation test with Microsoft Outlook"
.Body = "This is the body of the message." &vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance
' 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
 
Last edited:

Users who are viewing this thread

Back
Top Bottom