Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / September 2006

Tip: Looking for answers? Try searching our database.

Challenging: emailmerge attachment + body

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Albert - 18 Aug 2006 07:16 GMT
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
Peter Jamieson - 18 Aug 2006 14:01 GMT
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

> Hello!
> Here's a challenging question.
[quoted text clipped - 7 lines]
> Best Regards and thanks in advance,
> Albert
Albert - 18 Aug 2006 16:08 GMT
Hello Peter...
I don't know how to do this... Could you give me a hand?

> 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
Peter Jamieson - 18 Aug 2006 16:33 GMT
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

> Hello Peter...
> I don't know how to do this... Could you give me a hand?
[quoted text clipped - 6 lines]
> Thanks,
> Albert
Albert - 18 Aug 2006 16:56 GMT
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
Peter Jamieson - 18 Aug 2006 17:19 GMT
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

> Hi Peter.
> Thanks again for all your help.
[quoted text clipped - 7 lines]
> Thanks,
> Albert
Albert - 18 Aug 2006 17:28 GMT
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"...

> 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
[quoted text clipped - 17 lines]
> > Thanks,
> > Albert
Peter Jamieson - 18 Aug 2006 17:43 GMT
> 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
> Outlook 11 reference checked...
> I'm using Word 11 and Outlook 11.
[quoted text clipped - 24 lines]
>> > Thanks,
>> > Albert
Albert - 18 Aug 2006 17:55 GMT
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
Peter Jamieson - 18 Aug 2006 18:48 GMT
> 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

> OK, I got it to work...
> Already getting the messages with the appropiate body and attachment to my
[quoted text clipped - 8 lines]
> Thanks again,
> Albert
Albert - 18 Aug 2006 19:09 GMT
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
Peter Jamieson - 18 Aug 2006 19:25 GMT
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

>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
crazycaper - 20 Sep 2006 23:46 GMT
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.

> > 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
[quoted text clipped - 47 lines]
> >> > Thanks,
> >> > Albert
Peter Jamieson - 26 Sep 2006 11:43 GMT
Hi,

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

Did you do this:

>> >> 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).

?

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

Peter Jamieson
>I was following along, I need this macro as well. I got to the same error
>as
[quoted text clipped - 64 lines]
>> >> > Thanks,
>> >> > Albert
Albert - 18 Aug 2006 17:44 GMT
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...
Peter Jamieson - 18 Aug 2006 17:52 GMT
> 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

> 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...
Graham Mayor - 18 Aug 2006 14:13 GMT
See the article prepared by fellow MVP Doug Robbins at
http://word.mvps.org/FAQs/MailMerge/MergeWithAttachments.htm

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hello!
> Here's a challenging question.
[quoted text clipped - 4 lines]
> Best Regards and thanks in advance,
> Albert
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.