Email Merge with First Name in body & Subject

C

cleokolo

I'm trying to send a group of people an email. I personalize the
email with each person's name. Now I want to personalize the subject
line as well to each person. For example:

Word email merge using the merge field names:

Dear Susan (firstname)
Dear Jake (firstname)
Dear Sam
Dear Joe

email merge

My subject would be FirstName, Did you like the pictures I sent to
you (example).

Is there a way to include that merge field into the subject line? Or
do I have to continue to open each one up and manually type in the
person's name in each email. It get's old when you want to
personalize 20 or 30 or 50 emails.

Thank you.
 
P

Peter Jamieson

Is there a way to include that merge field into the subject line?

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean


' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.


Set objMerge = ActiveDocument.MailMerge
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"


' I don't use FirstRecord, LastRecord because they do not behave

' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False


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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same

' capitalisaiton as the name in the data source
.MailSubject = .DataSource.DataFields("FirstName").Value & _

"- did you like the pictures I sent to you?"
.Execute


intSourceRecord = intSourceRecord + 1
End If
Loop
End With


Peter Jamieson
 
P

Peter Jamieson

Re: Email Merge with First Name in body & SubjectJohn,

Thanks for giving that a once-over. At some point perhaps we'll find out whether the questioner is actually using a Mac :)

Peter Jamieson

Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested that it compiles on the Mac in Word 2004. Given that all of the objects are present in Word 2004 VBA, it should have a very high chance of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge to Email can be performed ONLY to Microsoft Entourage, which means Entourage must be set as the default email program on the Mac.

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.
Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

.MailSubject = .DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

--

Please reply to the newsgroup to maintain the thread. Please do not email me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Business Analyst, Consultant Technical Writer.
Sydney, Australia +61 (0) 4 1209 1410
 
P

Peter Jamieson

Re: Email Merge with First Name in body & SubjectUnfortunately, now I've been able to experiment, Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or attachment) (as on Windows Word) but VBA on Mac does not support the ..MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per record, merging to a new document then sending that via some automated method. However AFAIK .SendMail just opens a dialog rather than doing the sending and I don't know what other facilities for automating Entourage exist.

Peter Jamieson

John,

Thanks for giving that a once-over. At some point perhaps we'll find out whether the questioner is actually using a Mac :)

Peter Jamieson

Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested that it compiles on the Mac in Word 2004. Given that all of the objects are present in Word 2004 VBA, it should have a very high chance of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge to Email can be performed ONLY to Microsoft Entourage, which means Entourage must be set as the default email program on the Mac.

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.
Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

.MailSubject = .DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

--

Please reply to the newsgroup to maintain the thread. Please do not email me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Business Analyst, Consultant Technical Writer.
Sydney, Australia +61 (0) 4 1209 1410
 
J

Jim Gordon MVP

Hi Peter,

I confirm that the execute command as used in your example causes Word to
crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I
think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2,
DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"

'objMerge.MailSubject = .DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Unfortunately, now I've been able to experiment,
Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's
possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or attachment)
(as on Windows Word) but VBA on Mac does not support the .MailFormat property
that lets you choose programmatically.

The other general way to approach this problem is to do one merge per record,
merging to a new document then sending that via some automated method. However
AFAIK .SendMail just opens a dialog rather than doing the sending and I don't
know what other facilities for automating Entourage exist.

Peter Jamieson


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 
P

Peter Jamieson

Re: Email Merge with First Name in body & SubjectJim,

Thanks! Good old WordBasic, eh? I think they've even added stuff to the WordBasic object in Word 2007.

Peter Jamieson
Hi Peter,

I confirm that the execute command as used in your example causes Word to crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2, DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"

'objMerge.MailSubject = .DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:


Unfortunately, now I've been able to experiment, Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or attachment) (as on Windows Word) but VBA on Mac does not support the ..MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per record, merging to a new document then sending that via some automated method. However AFAIK .SendMail just opens a dialog rather than doing the sending and I don't know what other facilities for automating Entourage exist.

Peter Jamieson



John,



Thanks for giving that a once-over. At some point perhaps we'll find out whether the questioner is actually using a Mac :)



Peter Jamieson





Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested that it compiles on the Mac in Word 2004. Given that all of the objects are present in Word 2004 VBA, it should have a very high chance of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge to Email can be performed ONLY to Microsoft Entourage, which means Entourage must be set as the default email program on the Mac.

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.
Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

.MailSubject = ..DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub



--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 
J

Jim Gordon MVP

Hi Peter,

Office 2004 is the last version of Office that will support Visual Basic for
Applications directly. In the future you¹ll need to run VBA via a compiled
executive file such as AppleScript or RealBasic.

-Jim


Jim,

Thanks! Good old WordBasic, eh? I think they've even added stuff to the
WordBasic object in Word 2007.

Peter Jamieson
Hi Peter,

I confirm that the execute command as used in your example causes Word to
crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I
think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2,
DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"

'objMerge.MailSubject = .DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Quoting from "Peter Jamieson" <[email protected]>, in article
(e-mail address removed), on [DATE:

Unfortunately, now I've been able to experiment,
Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's
possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or
attachment) (as on Windows Word) but VBA on Mac does not support the
.MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per
record, merging to a new document then sending that via some automated
method. However AFAIK .SendMail just opens a dialog rather than doing the
sending and I don't know what other facilities for automating Entourage
exist.

Peter Jamieson



John,



Thanks for giving that a once-over. At some point perhaps we'll find out
whether the questioner is actually using a Mac :)



Peter Jamieson





message Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested
that it compiles on the Mac in Word 2004. Given that all of the
objects are present in Word 2004 VBA, it should have a very high chance
of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge
to Email can be performed ONLY to Microsoft Entourage, which means
Entourage must be set as the default email program on the Mac.

On 24/2/07 4:30 AM, in article (e-mail address removed),

Is there a way to include that merge field into the subject line?

As far as I know, only using VBA or another method of automating
Word.

In Word 2002/2003 on Windows there are Merge Events which allow you
to
change the .MailSubject attribute of the MailMerge object prior to
merging
each record. Those Events don't exist on Word Mac as far as I know,
but you
may be able to use VBA (or, say Applescript if that's your thing) to
do one
merge per record in the data source. I have only used the following
macro on
the Windows version so do not know if it will, or can, work on Mac.
Also, it
will only work if you are producing one e-mail per record in your
data
source. using stuff such as <<Next record>> fields will screw it up.

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA
expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the
same
' capitalisaiton as the name in the data source

.MailSubject = .DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to
you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 
D

Daiya Mitchell

side note--whenever I've gotten messages with my first name in the
subject line, they've been spam. Not sure your plan is worth the effort.
 
P

Peter Jamieson

Re: Email Merge with First Name in body & SubjectHi Jim,

Yes, I hope the various things that rely on WordBasic will still be do-able in the next release.

Peter Jamieson
Hi Peter,

Office 2004 is the last version of Office that will support Visual Basic for Applications directly. In the future you'll need to run VBA via a compiled executive file such as AppleScript or RealBasic.

-Jim


Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:


Jim,

Thanks! Good old WordBasic, eh? I think they've even added stuff to the WordBasic object in Word 2007.

Peter Jamieson


Hi Peter,

I confirm that the execute command as used in your example causes Word to crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2, DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"

'objMerge.MailSubject = ..DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:



Unfortunately, now I've been able to experiment, Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or attachment) (as on Windows Word) but VBA on Mac does not support the .MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per record, merging to a new document then sending that via some automated method. However AFAIK .SendMail just opens a dialog rather than doing the sending and I don't know what other facilities for automating Entourage exist.

Peter Jamieson




John,



Thanks for giving that a once-over. At some point perhaps we'll find out whether the questioner is actually using a Mac :)



Peter Jamieson






Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested that it compiles on the Mac in Word 2004. Given that all of the objects are present in Word 2004 VBA, it should have a very high chance of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge to Email can be performed ONLY to Microsoft Entourage, which means Entourage must be set as the default email program on the Mac.

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.
Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

.MailSubject = ..DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub



--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info





--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 
J

Jim Gordon MVP

If my understanding is correct (based on Word¹s help file) WordBasic is not
compiled. It¹s just a collection of about 900 commands. Since it is not
compiled there is at least a chance it will survive the upgrade.

-Jim Gordon
Mac MVP


Hi Jim,

Yes, I hope the various things that rely on WordBasic will still be do-able in
the next release.

Peter Jamieson
Hi Peter,

Office 2004 is the last version of Office that will support Visual Basic for
Applications directly. In the future you¹ll need to run VBA via a compiled
executive file such as AppleScript or RealBasic.

-Jim


Quoting from "Peter Jamieson" <[email protected]>, in article
(e-mail address removed), on [DATE:

Jim,

Thanks! Good old WordBasic, eh? I think they've even added stuff to the
WordBasic object in Word 2007.

Peter Jamieson


Hi Peter,

I confirm that the execute command as used in your example causes Word to
crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I
think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA
expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the
same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2,
DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to
you?"

'objMerge.MailSubject = .DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to
you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Quoting from "Peter Jamieson" <[email protected]>, in
article (e-mail address removed), on [DATE:



Unfortunately, now I've been able to experiment,
Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose
it's possible that that is "by design" to prevent automated mass
mailings.

Also interesting that the UI lets you pick HTML or plain text (or
attachment) (as on Windows Word) but VBA on Mac does not support the
.MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per
record, merging to a new document then sending that via some automated
method. However AFAIK .SendMail just opens a dialog rather than doing
the sending and I don't know what other facilities for automating
Entourage exist.

Peter Jamieson




John,



Thanks for giving that a once-over. At some point perhaps we'll find
out whether the questioner is actually using a Mac :)



Peter Jamieson






in message Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have
tested that it compiles on the Mac in Word 2004. Given that all of
the objects are present in Word 2004 VBA, it should have a very high
chance of working as designed :)

I am sure the questioner knows (because he has got this far) that
Merge to Email can be performed ONLY to Microsoft Entourage, which
means Entourage must be set as the default email program on the Mac.

On 24/2/07 4:30 AM, in article
(e-mail address removed), "Peter Jamieson"

Is there a way to include that merge field into the subject
line?

As far as I know, only using VBA or another method of automating
Word.

In Word 2002/2003 on Windows there are Merge Events which allow
you to
change the .MailSubject attribute of the MailMerge object prior
to merging
each record. Those Events don't exist on Word Mac as far as I know,
but you
may be able to use VBA (or, say Applescript if that's your thing)
to do one
merge per record in the data source. I have only used the
following macro on
the Windows version so do not know if it will, or can, work on
Mac. Also, it
will only work if you are producing one e-mail per record in your
data
source. using stuff such as <<Next record>> fields will screw it
up.

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA
expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has
the same
' capitalisaiton as the name in the data source

.MailSubject = .DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to
you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info


--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 
P

Peter Jamieson

Re: Email Merge with First Name in body & SubjectI don't know how WordBasic is implemented on either platform but on the Windows side it's presented as an Object that's a property of the Global/Application objects, and about which the Object Browser holds no further info. It's not clear whether every WordBasic command that manipulates Word actually invokes Word's COM object (or however it works on Mac) and that the reason why some WordBasic commands (such as the ones we're talking about) are not available in VBA is simply because they have not been exposed via the object library or some such. Nor is it completely clear /why/ some of these things have been implemented via Wordbasic rather than the published object model - perhaps it's precisely to avoid publishing a new interface because the longterm plan is not to have those objects, or maybe it's to circumvent some security problem or other.

In order to be able to use the WordBasic stuff I assume that the next version of (Mac) Word is either going to need to
a. provide access to the WordBasic object (I don't see that facility in the AppleScript dictionary at the moment, for example, but perhaps it is there). In addition, any language using that object would have to be syntactically capable of doing so. Or
b. provide direct access to the objects that WordBasic is using. At the moment I don't see that in AppleScript either in this case, but perhaps it's there somewhere.

Otherwise MS would be saying "you won't be able to automate that any more", and of course they may have their reasons for doing that too.

My own experience is that any developer including MS is quite capable of forgetting this kind of stuff until very late in the development day so I only mention it because I hear that the MacBU is watching :)

Peter Jamieson

If my understanding is correct (based on Word's help file) WordBasic is not compiled. It's just a collection of about 900 commands. Since it is not compiled there is at least a chance it will survive the upgrade.

-Jim Gordon
Mac MVP


Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:


Hi Jim,

Yes, I hope the various things that rely on WordBasic will still be do-able in the next release.

Peter Jamieson


Hi Peter,

Office 2004 is the last version of Office that will support Visual Basic for Applications directly. In the future you'll need to run VBA via a compiled executive file such as AppleScript or RealBasic.

-Jim


Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:



Jim,

Thanks! Good old WordBasic, eh? I think they've even added stuff to the WordBasic object in Word 2007.

Peter Jamieson



Hi Peter,

I confirm that the execute command as used in your example causes Word to crash. I tested Word 2004 and Word 2001. Both crashed.

After I made a slight modification, however, the code runs just fine. I think you can choose the format of the outgoing message. Here goes:

Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


objMerge.DataSource.FirstRecord = intSourceRecord
objMerge.DataSource.LastRecord = intSourceRecord
'objMerge.Destination = wdSendToEmail
' set up the field containing the e-mail address
objMerge.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

With ActiveDocument.MailMerge
.Destination = wdSendToEmail
.Execute
End With
WordBasic.DMMMergeToEMail DMMEMailSendAs:=2, DMMEMailTo:=2, DMMEMailSubject:=.DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"

'objMerge.MailSubject = ..DataSource.DataFields("First").Value & _
"- did you like the pictures I sent to you?"
'objMerge.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub

Sub test()
Set MyMerge = ActiveDocument.MailMerge
If MyMerge.State = wdMainAndDataSource Then MyMerge.Execute

End Sub


-Jim Gordon
Mac MVP

Quoting from "Peter Jamieson" <[email protected]>, in article (e-mail address removed), on [DATE:




Unfortunately, now I've been able to experiment, Activedocument.Mailmerge.Execute crashes Mac Word. Period. I suppose it's possible that that is "by design" to prevent automated mass mailings.

Also interesting that the UI lets you pick HTML or plain text (or attachment) (as on Windows Word) but VBA on Mac does not support the .MailFormat property that lets you choose programmatically.

The other general way to approach this problem is to do one merge per record, merging to a new document then sending that via some automated method. However AFAIK .SendMail just opens a dialog rather than doing the sending and I don't know what other facilities for automating Entourage exist.

Peter Jamieson





John,



Thanks for giving that a once-over. At some point perhaps we'll find out whether the questioner is actually using a Mac :)



Peter Jamieson







Hi Peter:

Very nice indeed :) I fixed a couple of typos (below) and have tested that it compiles on the Mac in Word 2004. Given that all of the objects are present in Word 2004 VBA, it should have a very high chance of working as designed :)

I am sure the questioner knows (because he has got this far) that Merge to Email can be performed ONLY to Microsoft Entourage, which means Entourage must be set as the default email program on the Mac.

As far as I know, only using VBA or another method of automating Word.

In Word 2002/2003 on Windows there are Merge Events which allow you to
change the .MailSubject attribute of the MailMerge object prior to merging
each record. Those Events don't exist on Word Mac as far as I know, but you
may be able to use VBA (or, say Applescript if that's your thing) to do one
merge per record in the data source. I have only used the following macro on
the Windows version so do not know if it will, or can, work on Mac. Also, it
will only work if you are producing one e-mail per record in your data
source. using stuff such as <<Next record>> fields will screw it up.
Sub ProduceOneEmailPerSourceRec()
'

' NB, needs bettor error management and doubtless other things a VBA expert
' will point out.


Dim intSourceRecord
Dim objMerge As Word.MailMerge
Dim bTerminateMerge As Boolean

' 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"

' Need to set up this object as the ActiveDocument changes when the
' merge is performed. Besides, it's clearer.

Set objMerge = ActiveDocument.MailMerge
With objMerge

' I don't use FirstRecord, LastRecord because they do not behave
' the way you expect in all data sources.

intSourceRecord = 1
bTerminateMerge = False

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


.DataSource.FirstRecord = intSourceRecord
.DataSource.LastRecord = intSourceRecord
.Destination = wdSendToEmail
' set up the field containing the e-mail address
.MailAddressFieldName = "eaddress"
' Set up the subject - make sure any field name in here has the same
' capitalisaiton as the name in the data source

.MailSubject = ..DataSource.DataFields("FirstName").Value & _
"- did you like the pictures I sent to you?"
.Execute
intSourceRecord = intSourceRecord + 1
End If
Loop
End With
End Sub



--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info





--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info





--
Jim Gordon
Mac MVP

MVPs are not Microsoft Employees
MVP info
 

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