email merge from MSWord

G

Guest

I wish to send emails every month to 20 recipients which contain invites
addressed to each recipient as an attachment(the invite has a couple of
images and also contains each recipient's name for a personal touch.)I have
tried merging the email messages from MSWord and using MS Oulook to send
them.
How can I use the mail merge option to include the message to each recipient
telling them that an invite has been attached?That is, I want both the email
message and invite attachment to be mail merged from MSWord simultaneously.
Is it possible?
Thanks.
 
P

Peter Jamieson

There is no facility to do this "out of the box" but you can use a macro, e.g. Doug Robbins' approach at

http://word.mvps.org/FAQs/MailMerge/MergeWithAttachments.htm

or a Word VBA macro such as the one posted below:

You have to make a reference to Microsoft Outlook 11.0 Object Library (or
the appropriate version of the library) in the VB Editor when this module is
open.

Sub EmailOneDocPerSourceRecWithBody()
' By Peter Jamieson, 2006
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


bOutlookStarted = False
bTerminateMerge = False


' Set up a reference to the
' Activedocument, partly because
' the ActiveDocument changes as you
' merge each record


Set objMerge = ActiveDocument.MailMerge


' Start Outlook as necessary


On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set objOutlook = CreateObject("Outlook.Application")
bOutlookStarted = True
End If


With objMerge


' If no data source has been defined,
' do it here using OpenDataSource.
' But if it is already defined in the
' document, you should not need to
' define it here.


' .OpenDataSource _
' Name:="whatever"


intSourceRecord = 1


Do Until bTerminateMerge
.DataSource.ActiveRecord = intSourceRecord


' if we have gone past the end
' (and possibly, if there are no records)
' then the Activerecord will not be what
' we have just tried to set it to


If .DataSource.ActiveRecord <> intSourceRecord Then
bTerminateMerge = True
' the record exists
Else


' while we are looking at the
' correct activerecord,
' create the mail subject, body and "to"
' Just some sample code here - replace it with
' whatever you need


strMailSubject = _
"Results for " & _
objMerge.DataSource.DataFields("Firstname") & _
" " & objMerge.DataSource.DataFields("Lastname")


strMailBody = _
"Dear " & objMerge.DataSource.DataFields("Firstname") & _
vbCrLf & _
"Please find attached a Word document containing" & vbCrLf & _
"your results for..." & vbCrLf & _
vbCrLf & _
"Yours" & vbCrLf & _
"Your name"
strMailTo = objMerge.DataSource.DataFields("Emailaddress")


' create the document path name
' In this case it can be the same for every recipient,
' but if you want to retain copies of the
' document, you can use info. in the data source


' this is an example - insert your
' own pathname here


strOutputDocumentName = "c:\a\results.doc"


' strOutputDocumentName = _
' "c:\mymergeletters\_" & _
' .DataSource.DataFields("Lastname").Value & _
' " letter.doc"
.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToNewDocument
.Execute


' The Activedocument is always the
' output document


' Add any parameters you need to these calls
ActiveDocument.SaveAs strOutputDocumentName
ActiveDocument.Close


' Now create a


Set objMailItem = objOutlook.CreateItem(olMailItem)
With objMailItem
.Subject = strMailSubject
.Body = strMailBody
.To = strMailTo
.Attachments.Add strOutputDocumentName, olByValue, 1
'.Save
.Send
End With
Set objMailItem = Nothing


intSourceRecord = intSourceRecord + 1
End If
Loop
End With


' Close Outlook if appropriate


If bOutlookStarted Then
objOutlook.Quit
End If


Set objOutlook = Nothing
Set objMerge = Nothing


End Sub


Peter Jamieson
 
G

Guest

Peter,

This is incredibly helpful to a problem I was looking for help on. The only
problem I'm now having is that I can get it to work and attach a pdf, but the
recipient gets a message that the pdf attachment is corrupt and wrongly
encoded.

Any ideas??

Thanks
Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top