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