Dear Otto,
I try to use oWord:Mailmerge in my program as customer requirement for make rental contract. I got this code in the forum and modify to support in the future by create temporary file for preparing data (MergeField in Word) and can add or modify in future case.
Otto wrote:Hello Dutch,
would you be so kind to post some code.
Thank you in advance and best regards,
Otto
Code: Select all
*---------------------------------*
Procedure DocMerge( cForm, cFile )
local oWord, oDoc, cOutFile, cTmpDbf, aTmpDbf, OkToWord , cName
local aDataField, aDatas, n
CursorWait()
cOutFile := cFile // "d:\fwh1312\samples\testdoc.doc"
cTmpDbf := MEMVAR->cFoPath+"GSTINFO.DBF" // "d:\fwh1312\samples\guest.dbf"
cFile := MEMVAR->cFoPath+"LTRFORM\"+cForm // "d:\fwh1312\samples\Rental.doc"
aTmpDbf := {}
aDatas := {}
DbUseArea(.T.,,"EZMERGE","MRG",.T.)
do while !MRG->(eof())
aadd( aTmpDbf, {rtrim(upper(MRG->MRG_NAME)), MRG->MRG_TYPE, MRG->MRG_LENGTH, MRG->MRG_DEC} )
aadd( aDatas , {rtrim(upper(MRG->MRG_NAME)), rtrim(MRG->MRG_DATA)} )
MRG->(DbSkip())
end
MRG->(DbCloseArea())
if file( cTmpDbf )
ferase( cTmpDbf )
end
if !empty( aTmpDbf )
DbCreate( cTmpDbf, aTmpDbf )
sleep(1000)
DbUseArea(.T.,,"GSTINFO","GSI",.T.)
GSI->(DbAppend())
if GSI->(Rlock())
for n := 1 to len(aDatas)
&('GSI->'+aDatas[n][1]) := &(aDatas[n][2])
next
end
GSI->(DbCommit())
GSI->(DbCloseArea())
oWord := CreateObject("Word.Application")
oWord:Set("Visible",.f.)
oDoc:=oWord:Get( "Documents" )
oWord:Set("DisplayAlerts",0)
oWord:Set("Visible",.F.)
oWord:Set( "WindowState", 2 ) // Minimize
oDoc:Open(cFile)
oWord:ActiveDocument:MailMerge:MainDocumentType := 0 //wdFormLetters=0 // sets the mail merge main document type
oWord:ActiveDocument:MailMerge:EditMainDocument()
oWord:ActiveDocument:MailMerge:OpenDataSource(cTmpDbf) //TmpDbf is the path&file name of temp database
// oWord:ActiveDocument:MailMerge:Destination( 1 ) // 0=File, 1=Printer, 2=Email, 3=Fax
oWord:ActiveDocument:MailMerge:Execute( .F. )
cName:=oWord:Get("ActiveDocument")
//about to save check to see if possible or if someone else
//has file open
cName:SaveAs(cOutFile)
IF cName:Saved() == .F.
MsgStop("Save to Word Fail")
cOutFile := ''
ENDIF
oWord:Documents:Close(.T.)
oWord:Quit()
if file(cOutFile)
ShellExecute(0, 'Open', cOutFile,,4,0)
end
end
CursorArrow()
return