pk247
03-25-2015, 03:29 PM
Hi All,
A while ago I tweaked the code below to attach the mail merged letter as a pdf file to the email with pre-written subject and body (I have edited this out for compliance reasons). It works fine but with a new update in my workplace the pdf files no longer open because of Adobe settings (corrupt file etc.).
I'm sure there's a way around this but I'm just not competent enough to get it to work. Would anyone be able to help me with this please? I'm on Word 2010.
Sub Email_Pdf_as_attachment()
' reference to the Microsoft Office of Outlook Library
' reference to the Microsoft Scripting Runtime
' ensure Z drive folder is set up for pdf files
Dim bOutlookStarted As Boolean
Dim bTerminateMerge As Boolean
Dim intSourceRecord As Integer
Dim objMailItem As Outlook.MailItem
Dim objMerge As Word.MailMerge
Dim objOutlook As Outlook.Application
Dim strMailSubject As String
Dim strMailTo As String
Dim strMailBody As String
Dim strOutputDocumentName As String
Dim savePath As String
bOutlookStarted = False
bTerminateMerge = False
Set objMerge = ActiveDocument.MailMerge
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set objOutlook = CreateObject("Outlook.Application")
bOutlookStarted = True
End If
With objMerge
intSourceRecord = 1
Do Until bTerminateMerge
.DataSource.ActiveRecord = intSourceRecord
If .DataSource.ActiveRecord <> intSourceRecord Then
bTerminateMerge = True
Else
strMailSubject = "Email Subject..." & _
objMerge.DataSource.DataFields("Type_Indicator")
strMailBody = "Dear " & objMerge.DataSource.DataFields("First_Name").Value & " " & _
objMerge.DataSource.DataFields("Last_Name").Value & "," & _
"<p>Please find attached..." & vbNewLine & _
"<p>If you require any further assistance... " & _
"<p>Yours sincerely," & _
"<p>Name..."
strMailTo = objMerge.DataSource.DataFields("Email")
savePath = ActiveDocument.Path
strOutputDocumentName = "Z:\Downloads\" & _
.DataSource.DataFields("First_Name").Value & "_" & _
.DataSource.DataFields("Last_Name").Value & _
"...Access Letter.pdf"
.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToNewDocument
.Execute
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Set objMailItem = objOutlook.CreateItem(olMailItem)
With objMailItem
.BodyFormat = olFormatHTML
.Subject = strMailSubject
.HTMLBody = strMailBody
.To = strMailTo
.Attachments.Add strOutputDocumentName, olByValue, 1
.Save
.Send
End With
Set objMailItem = Nothing
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
If bOutlookStarted Then
objOutlook.Quit
End If
Set objOutlook = Nothing
Set objMerge = Nothing
End Sub
PS If I change the .pdf in "...Access Letter.pdf" to .doc the attachment opens just fine in Word
Thanks in advance for any assistance.
Paul, Ireland
A while ago I tweaked the code below to attach the mail merged letter as a pdf file to the email with pre-written subject and body (I have edited this out for compliance reasons). It works fine but with a new update in my workplace the pdf files no longer open because of Adobe settings (corrupt file etc.).
I'm sure there's a way around this but I'm just not competent enough to get it to work. Would anyone be able to help me with this please? I'm on Word 2010.
Sub Email_Pdf_as_attachment()
' reference to the Microsoft Office of Outlook Library
' reference to the Microsoft Scripting Runtime
' ensure Z drive folder is set up for pdf files
Dim bOutlookStarted As Boolean
Dim bTerminateMerge As Boolean
Dim intSourceRecord As Integer
Dim objMailItem As Outlook.MailItem
Dim objMerge As Word.MailMerge
Dim objOutlook As Outlook.Application
Dim strMailSubject As String
Dim strMailTo As String
Dim strMailBody As String
Dim strOutputDocumentName As String
Dim savePath As String
bOutlookStarted = False
bTerminateMerge = False
Set objMerge = ActiveDocument.MailMerge
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set objOutlook = CreateObject("Outlook.Application")
bOutlookStarted = True
End If
With objMerge
intSourceRecord = 1
Do Until bTerminateMerge
.DataSource.ActiveRecord = intSourceRecord
If .DataSource.ActiveRecord <> intSourceRecord Then
bTerminateMerge = True
Else
strMailSubject = "Email Subject..." & _
objMerge.DataSource.DataFields("Type_Indicator")
strMailBody = "Dear " & objMerge.DataSource.DataFields("First_Name").Value & " " & _
objMerge.DataSource.DataFields("Last_Name").Value & "," & _
"<p>Please find attached..." & vbNewLine & _
"<p>If you require any further assistance... " & _
"<p>Yours sincerely," & _
"<p>Name..."
strMailTo = objMerge.DataSource.DataFields("Email")
savePath = ActiveDocument.Path
strOutputDocumentName = "Z:\Downloads\" & _
.DataSource.DataFields("First_Name").Value & "_" & _
.DataSource.DataFields("Last_Name").Value & _
"...Access Letter.pdf"
.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToNewDocument
.Execute
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Set objMailItem = objOutlook.CreateItem(olMailItem)
With objMailItem
.BodyFormat = olFormatHTML
.Subject = strMailSubject
.HTMLBody = strMailBody
.To = strMailTo
.Attachments.Add strOutputDocumentName, olByValue, 1
.Save
.Send
End With
Set objMailItem = Nothing
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
If bOutlookStarted Then
objOutlook.Quit
End If
Set objOutlook = Nothing
Set objMerge = Nothing
End Sub
PS If I change the .pdf in "...Access Letter.pdf" to .doc the attachment opens just fine in Word
Thanks in advance for any assistance.
Paul, Ireland