Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
albert albert is offline
external usenet poster
 
Posts: 25
Default Challenging: emailmerge attachment + body

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
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Challenging: emailmerge attachment + body

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

"Albert" wrote in message
...
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



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
albert albert is offline
external usenet poster
 
Posts: 25
Default Challenging: emailmerge attachment + body

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

"Peter Jamieson" wrote:
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
  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Challenging: emailmerge attachment + body

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

"Albert" wrote in message
news
Hello Peter...
I don't know how to do this... Could you give me a hand?

"Peter Jamieson" wrote:
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



  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
albert albert is offline
external usenet poster
 
Posts: 25
Default Challenging: emailmerge attachment + body

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


  #6   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Peter Jamieson Peter Jamieson is offline
external usenet poster
 
Posts: 4,582
Default Challenging: emailmerge attachment + body

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

"Albert" wrote in message
...
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



  #7   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Graham Mayor Graham Mayor is offline
external usenet poster
 
Posts: 19,312
Default Challenging: emailmerge attachment + body

See the article prepared by fellow MVP Doug Robbins at
http://word.mvps.org/FAQs/MailMerge/...ttachments.htm

--

Graham Mayor - Word MVP

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


Albert wrote:
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



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mail merge to email - message body AND attachment Dan Mailmerge 1 May 26th 06 05:48 PM
Body Text vs Normal Adrian Formatting Long Documents 13 March 9th 06 05:01 AM
Printing an Attachment containing {FORMTEXT} Bernard Littman New Users 4 February 24th 06 12:18 PM
Please Help! (Mail merge e-mailing with images in Body) [email protected] Mailmerge 2 January 17th 06 03:25 PM
option send to e-mail address (as attachment) unavailable NotAnExpert Microsoft Word Help 1 April 20th 05 03:36 PM


All times are GMT +1. The time now is 10:50 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"