Challenging: emailmerge attachment + body

A

Albert

Hello!
Here's a challenging question.
When merging to email, Word offers the option of merging the document as an
attachment or as email body text. Is there a way of merging the document as
an attachment AND adding some text to the body of the email so as to not to
send a text-less email with an attachment?
Best Regards and thanks in advance,
Albert
 
P

Peter Jamieson

For a reasonably simple case you could use a macro such as the following.

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()
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 te 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
 
A

Albert

Hello Peter...
I don't know how to do this... Could you give me a hand?

Peter Jamieson said:
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.

Thanks,
Albert
 
P

Peter Jamieson

OK, assuming you have copied the macro to a module in your Word document,
a. open the VB Editor
b. open the module
c. go to Tools|References. Look down the list for Microsoft Outlook 11.0
Object Library, check the box to the left of that item, then click OK

If you are using Outlook 2002, you'll need Microsoft Outlook 10.0 Object
Library instead, etc. etc.

Peter Jamieson
 
A

Albert

Hi Peter.
Thanks again for all your help.
Did what you just told me.
I get errors in these two lines:

Dim objMailItem As outlook.MailItem
Dim objOutlook As outlook.Application

what to do?
Thanks,
Albert
 
P

Peter Jamieson

First, have a look under Tools|references again and ensure that the
Microsoft Outlook <whatever> library is checked (it will be near the top of
the list if it is).

What error message is displayed?

Which versions of Word and Outlook?

Peter Jamieson
 
A

Albert

Outlook 11 reference checked...
I'm using Word 11 and Outlook 11.
I get a compilation error... I'm using the spanish version of Office, so I
don't know exactly how to translate it. However, it says something about
"defined type has not been defined by user"...
 
P

Peter Jamieson

I get a compilation error... I'm using the spanish version of Office, so I
don't know exactly how to translate it. However, it says something about
"defined type has not been defined by user"...

OK. For future reference it is usually useful to quote the error in the
original language (at least for languages that use the latin alphabet).

Assuming that the Intellisense feature is switched on in the VB Editor, can
you start typing

Dim objMailItem As

then see what options are offered. You are obviously looking for something
like "Outlook." I would not normally expect this kind of thing to be
different in different language versions of Windows or Office, but perhaps
this is an exception. (e.g. does Outlook have a different name in the
Spanish language version?)

If you can get as far as "Outlook", type a "." then see what else is
offered. Again, I would expect "MailItem", but perhaps it is something else.
The same applies to the other definition.

Peter Jamieson
 
A

Albert

Sorry about that, my bad... Wasn't checked... sometimes "office" looks like
"outlook". Stopped getting the error message.
It didn't work however...
There's nothing in my outlook out box...
 
P

Peter Jamieson

It didn't work however...
There's nothing in my outlook out box...

Works here.

Assuming you're not seeing any error messages, that suggests that
a. there are no other problems in the routine
b. the routine is finishing

These are both good signs.

During testing, the thing most likely to result in a "no-show" with no
messages produced seems to be that the names of field names differ in some
way from the names in your data source. If your data source uses
"FirstName", you have to use "FirstName", not "Firstname", etc. etc.

My best guess...

Peter Jamieson
 
A

Albert

OK, I got it to work...
Already getting the messages with the appropiate body and attachment to my
Outlook Outbox...
WE'RE getting THERE...
However, the attachments had the format all messed up. I think that it
merged the attachments to a different template from the one I'm using, or
something like that...
Two examples of the messed up format:
Double line spacing; where as in my merge document I used single.
Portrait orientation; where as in my merge document I used Landscape.
Thanks again,
Albert
 
P

Peter Jamieson

However, the attachments had the format all messed up. I think that it
merged the attachments to a different template from the one I'm using, or
something like that...

Hmm, that's worrying...

What happens if you open the (last) output document saved during the merge
(in my example, I think it would be c:\a\result.doc) ? Does it look the same
as the document you open from the e-mail? Does it look how you expect? Is it
attached to the template you expect? Are you opening the e-mail attachments
on the same machine where you produced them, or a different one?

Are you actually starting from a .dot or a .doc? (generally speaking I tend
to start from a .doc attached to Normal.dot these days).

Peter Jamieson
 
A

Albert

I was wrong again...
The documents are merged fine. However, by default, when I open the
attachment it is in [spanish] "reading form view".
I appreciate very much what you've done for me...
Thanks again for your time and efforts...
Albert
 
P

Peter Jamieson

Hi Albert,

I'm not sure what you can do about the view: it may or may not be anything
to do with the way that the .doc has been prepared and transmitted. To be
honest, I probably do not even notice what view things open in any more,
because I am so used to having to change it to the one I want to use. That's
not good, but it may be "the way it is".

Apart from that, it sounds as if progress has been good!

Peter Jamieson
 
C

crazycaper

I was following along, I need this macro as well. I got to the same error as
him (running an English versian) I got the same compilation error.

Dim objMailItem As Outlook.MailItem Isn't taking Outlook doesn't exsist, I'm
curious to what Albert ended up going with, as he didn't say in his reply.
 
P

Peter Jamieson

Hi,

Just back from a trip, hence delay. Saw your other message.

Did you do this:

?

It's best if you make the reference when the module containing the routine
is open in VBE.

Peter Jamieson
 

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