Here is the exact source I made :
- Code: Select all Expand view
- ******************************************************************************
#INCLUDE "FIVEWIN.CH"
#DEFINE DEFAULT(xVar,xVal) xVar=IF(xVar=NIL,xVal,xVar)
#DEFINE wdCollapseEnd 0
******************************************************************************
FUNCTION MAIN(cInput)
LOCAL cDisk := DiskName()
LOCAL cDir := CurDir()
LOCAL cTel := 0
DEFAULT(cInput,"1")
PRIVATE oWord,oDoc,oSel
TRY
oWord := TOleAuto():New("Word.Application")
* oWORD := CreateObject("word.Application")
oWord:Run("JUD_CHECK")
CATCH
MSGSTOP("Word is niet op deze computer geïnstalleerd !!!")
RETURN(.T.)
END
IF FILE(ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC")
IF VAL(cInput) = 1 .OR. VAL(cInput) = 2
oDoc := oWord:Documents:Open(ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC")
oDoc:Select()
oSel := oWord:Selection
cTel := WORDREPLACE(oSel,"AAAAA","BBBBB",VAL(cInput))
* oDoc:PrintOut()
oDoc:Save()
oDoc:Close(0)
MsgAlert("De tekstvervanging werd uitgevoerd !!!" + CHR(13) + CHR(13) + "Er werden " + ALLTRIM(STR(cTel,10,0)) + " vervangingen uitgevoerd.","Opgelet")
ELSE
MsgAlert("U heeft een foutieve input opgegeven !!!","Opgelet")
ENDIF
ELSE
MsgAlert("Het bestand " + ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC" + " bestaat niet","Opgelet")
ENDIF
oWord:Quit()
Quit
RETURN(.T.)
******************************************************************************
FUNCTION WORDREPLACE(oSel,cSrc,cRpl,cSrt)
LOCAL lOk := .F.
LOCAL lTel := 0
LOCAL oRng
DEFAULT(cSrt,1)
MsgInfo("OK1")
* cSrt = 1 : tekst wordt overal vervangen, ook in header en footer
* cSrt = 2 : tekst wordt overal vervangen, maar niet in header en footer
IF cSrt = 1
oRng := oSel:Document:Content
IF AT(cSrc,oRng:Text) > 0
DO WHILE oRng:Find:Execute(cSrc)
oRng:Text = cRpl
oRng:Collapse(wdCollapseEnd)
lTel++
ENDDO
ENDIF
ELSEIF cSrt = 2
oRng = oSel:StoryRanges[7]
IF AT(cSrc,oRng:Text) > 0
DO WHILE oRng:Find:Execute(cSrc)
oRng:Text := cRpl
oRng := oSel:StoryRanges[7]
lTel++
ENDDO
lOk = .T.
ENDIF
ENDIF
MsgInfo("OK2")
RETURN(lTel)
******************************************************************************
Thank you very much in advance.