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
*---------------------------------*
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