I wish to generate a new Word document (ALL.DOC) with all my customer´s labels.
So I have a template, LABELS.DOC, and I have to add it to the Word document and next replace the fields for each Customer.
But I only get a page with the template loaded.
This is the code:
- Code: Select all Expand view
- #include "FiveWin.ch"
#include "xbrowse.ch"
#DEFINE wdCollapseEnd 0
//----------------------------------------------------------------------------//
FUNCTION MAIN()
LOCAL oWord := CREATEOBJECT( "Word.Application" )
LOCAL oSel
oWord:Documents:Open( HB_DIRBASE()+"NEWDOC.DOC" ):Select()
// First customer
oSel = oWord:Selection
oSel:InsertFile(HB_DIRBASE()+"LABEL-TEMPLATE.DOC")
WORDREPLACE( oSel, "<MyField>", "Replaced Cusomer 1" )
// Second customer
oSel:InsertFile(HB_DIRBASE()+"LABEL-TEMPLATE.DOC")
WORDREPLACE( oSel, "<MyField>", "Replaced Cusomer 2" )
// etc, I will put a do while !eof()
oWord:Visible = .T.
oWord:Documents:Close(0)
oWord:Quit()
RETURN NIL
// Those functions are from (c) Enrico .it
FUNCTION WORDREPLACE( oSel, cSrc, cRpl )
LOCAL oRng := oSel:Document:Content
IF AT( cSrc, oRng:Text ) = 0; RETURN .F.; ENDIF
oRng:Find:Execute( cSrc )
oRng:Text = cRpl
RETURN .T.
FUNCTION WORDREPLACEALL( oSel, cSrc, cRpl )
LOCAL oRng := oSel:Document:Content
IF AT( cSrc, oRng:Text ) = 0; RETURN .F.; ENDIF
WHILE oRng:Find:Execute( cSrc )
oRng:Text = cRpl
oRng:Collapse( wdCollapseEnd )
ENDDO
RETURN .T.
//----------------------------------------------------------------------------//
Thank you very much for your help.
Kind regards