I'm trying to automate a mail merge using word in my app and need some help. Originally, i opened a doc that contained a piece of VB code that automated the mail merge, however this only worked with Word 2000 and not the later versions so i have decided to try and automate it through my app.
My app creates a temporary dbf that could contain multiple records.
I open the word doc
I then want to run the mail merge routine
Then save the word doc as another file name
One word document should be created that could merge multiple records.
my code so far looks like this:
- Code: Select all Expand view
oWord := CreateObject("Word.Application")
oWord:Set("Visible",.f.)
cFile := mTempDir+Alltrim(TmpFile)
oDoc:=oWord:Get( "Documents" )
oWord:Set("DisplayAlerts",0)
oWord:Set("Visible",.F.)
oWord:Set( "WindowState", 2 ) // Minimize
oDoc:Open(cFile)
oDoc:MailMerge:MainDocumentType := 0 //wdFormLetters=0 // sets the mail merge main document type
oDoc:MailMerge:EditMainDocument()
oDoc:MailMerge:OpenDataSource(TmpDbf) //TmpDbf is the path&file name of temp database
oDoc:MailMerge:Execute()
cName:=oWord:Get("ActiveDocument")
//about to save check to see if possible or if someone else
//has file open
IF FILE(mQuotNbr)
IF FERASE(mQuotNbr) == 0
OkToWord := .T.
ELSE
OkToWord := .F.
ENDIF
ENDIF
IF OkToWord
cName:SaveAs(mQuotNbr)
ENDIF
IF cName:Saved() == .F.
info_box(3,"205",.F.)
ENDIF
oWord:Documents:Close(.F.)
oDoc:Open(mQuotNbr)
oWord:Set("Visible",.T.)
oWord:Set( "WindowState", 1 ) // Maximize
When i try the above code i get:
Error description: Error 1564412/9 'Item' is not a property.: MAILMERGE
Thanks in advance
Pete