Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mailmerge automation doesn't work in Word 2003

Status
Not open for further replies.

TrogTuite

Programmer
Dec 11, 2000
6
GB
Doing a mailmerge using the following code works with Office 2000 but not Office 2003 and gives this error:
1429 OLE IDispatch exception code 0 from Microsoft Word: 'Destination' is not a by reference property... .Destination = wdSendToNewDocument

Any suggestions?

*-------------------------------------------------
#Define CR CHR(13)
#Define wdWindowStateNormal 0
#Define wdWindowStateMaximise 1
#Define wdOpenFormatAuto 0
#Define wdSendToNewDocument 0
#Define wdSendToEmail 2
#Define wdDefaultFirstRecord 1
#Define wdDefaultLastRecord -16
#Define wdDoNotSaveChanges 0

Release ALL LIKE o*
Erase C:\AA_VFP\BOOKINGFORM.DOC
Public oWord
Local oDoc, oRange, cText, oHeaderStyle

oWord = CreateObject("Word.Application")
*!* oWord.Visible = .T.

oWord.Documents.Open("C:\AA_VFP\BFmerge.doc")
With oWord.ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = .t.
*!* .MailAddressFieldName = "ptuite@btconnect.com"
.MailSubject = "This email generated by animal Aunts Booking System"
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
Endwith
.Execute(.f.)
Endwith
oWord.ActiveDocument.SaveAs("C:\AA_VFP\BOOKINGFORM.DOC")
*!* oWord.Options.SendMailAttach = .t.
*!* oWord.ActiveDocument.SendMail
oWord.Quit(wdDoNotSaveChanges)
Release oWord


Patrick Tuite
 
Patrick,

I only have Word 2000 so my advice can only be limited, but I often use Word's macro recording facility to find out what syntax, properties & methods to use with automation.

I would suggest you try this. Start the macro recording and go through your mailmerge in Word. Once you have set where the mailmerge should go to, stop the recorder and then go into macros and see what syntax it shows you. Obviously the syntax will be in VBA but you should be able to translate it to VFP, especially if you are using VFP7 or higher with Intellisense.

Hope that helps, let me know if you don't know how to use the macro recorder.

Stewart
 
Patrick,

If Stewart's advice works, please post here so that I (and others) know how to modify our mailmerge's so they'll work in both Word 2000 & 2003. (I only have 2000, also)
 
I wrote this for a French website a while back, the this seems to work on both versions of Word.
[code#Define wdPageBreak 7

Set Date Long &&Pour écrire la date en texte
Set Century On && Pour montrer l'année à quatre caracteres
Create Cursor monCurseur (nom c(20),address c(20),ville c(20),Pays c(20)) && Créer un Curseur
Insert Into monCurseur (nom,address,ville,Pays) Values ("John Pozer","123 NullePart","Cette Ville","Moldovia")
Insert Into monCurseur (nom,address,ville,Pays) Values ("Frank Smith","123 QuelquePart","Autre Ville","Canada")
oWord = Createobject("Word.application") && Instancier Word
loDocument = oWord.documents.Add() && Ajouter un nouveau document
loSelection = oWord.Selection && Sélectionner le document pour l'activer
loStr = "Microsoft Visual FoxPro version 8.0 est un outil très puissant permettant de développer "+;
"rapidement des applications et des composants de base de données."+;
" Son langage orienté objet et centré sur les données offre aux développeurs une suite "+;
"d’outils fiables pour la création d’applications de base de données pour un poste,"+;
"en environnement client/serveur ou sur le Web à l’aide de composants et de services Web XML."+;
" Ecoutez parler Eric Rudder au sujet de Visual FoxPro 8.0. Dans cette vidéo hébergée sur"+;
" le site GotDotNet, Eric Rudder, senior vice-president of the Developer and Platform Evangelism"+;
" Division at Microsoft, souligne les nouvelles possibilités de Visual FoxPro 8.0. Cette page,"+;
" contient aussi d'autres vidéos qui présentent l'intégration de VFP dans un environnement .NET." && Du texte dans une variable
Iif(Reccount('monCurseur')=1,undocument(),deuxetplus(Reccount())) && Déterminer combien de records le curseur contient
oWord.Visible = .T.
Procedure undocument
insertion() && Simple insertion
Endproc
Procedure deuxetplus && Insertion multiple
Lparameters nCount
Private x
x = 0
Select monCurseur
Scan
insertion()
x = x + 1
If x < nCount
loSelection.InsertBreak(wdPageBreak) && Si l'on est pas au dernier record insérer une nouvelle page
Endif
Endscan
Endproc

Procedure insertion
Local ldDate
ldDate = Dtoc(Date())
With loSelection
.typetext(ldDate)
.Typeparagraph && changer de ligne
.Typeparagraph && changer de ligne
.typetext(monCurseur.nom) && Valeur du curseur
.Typeparagraph
.typetext(monCurseur.address)
.Typeparagraph
.typetext(Alltrim(monCurseur.ville)+", "+Alltrim(monCurseur.Pays))
.Typeparagraph
.Typeparagraph
.typetext("Cher monsieur/madame "+ monCurseur.nom )
.Typeparagraph
.Typeparagraph
.typetext("Bienvenu au site web .Typeparagraph
.typetext(loStr) && Le contenu de la variable
.Typeparagraph
.Typeparagraph
.Typeparagraph
.typetext("Bill Gates")
.Typeparagraph
.typetext("Microsoft Corp.")
Endwith
Endproc
[/code]


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Patrick,

I'm using following code ( found somewhere on Internet ) :

#DEFINE wdMainAndDataSource 2
#DEFINE wdWindowStateMinimize 2
#DEFINE wdWindowStateMaximize 1
#DEFINE wdWindowStateNormal 0
#DEFINE OLEServerBusyRaiseError=.T.
#DEFINE OLEServerBusyTimeout = 5000
#DEFINE OLERequestPendingTimeout = 5000
#Define wdDefaultFirstRecord 1
#Define wdDefaultLastRecord -16

PRIVATE OleApp
OleApp = CreateObject("Word.Application")
WITH OleApp
.DisplayAlerts = .F.
.Visible=.T.
.Documents.Open("MyDoc.DOC")
ENDWITH

** check to make sure the documnet is attached to a
** datasource and ready to merge.
IF OleApp.ActiveDocument.MailMerge.State = wdMainAndDataSource
OleApp.ActiveDocument.MailMerge.DataSource.FirstRecord = wdDefaultFirstRecord
OleApp.ActiveDocument.MailMerge.DataSource.LastRecord = wdDefaultLastRecord
OleApp.ActiveDocument.MailMerge.Execute
OleApp.WindowState = wdWindowStateMaximize
ENDIF

It works correct for WORD97, 2000 and XP - Destination ToNewDocument looks as default
Option and isn't necesarry to set up..
Word2003 Mailmerge doing if ...MailMerge.State = wdMainAndDataSource in time, when
CreateObject("Word.App..") without respect aditional parameters ...Datasource.FirstRecord....etc.
It looks that default option for Destination is not ToNewDocument. This code create merged
documents but only with first record from datasource. I didn't find list of WORD2003 constants
to say Merge for All records ..... I'm still looking ...

Sorry for my english.
 
mailmerge using Office 2003 works with Shellexecute....
More faq184-3495.
 
axof

The link you posted does not link to any faq.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top