Merke: Sonderzeichen sind für Word jeweils ein eigenes Wort. "@NAME" sind zwei Wörter, also @ und NAME.
Here we go:
Code: Alles auswählen
FUNCTION WordReplace(cMeinText)
local oWord, oDoc, oWords, lHabePlatzHalter
oWord := CreateObject("Word.Application")
IF Empty( oWord )
MsgBox( "Microsoft Word ist nicht installiert" )
return nil
ENDIF
oWord := oWord:dynamicCast(ActiveXObject())
oWord:Visible := .F.
oWord:documents:open(cMeinText)
oDoc := oWord:ActiveDocument
oWords := oDoc:Words // alle Wörter des Dokuments
lHabePlatzHalter := .F. // wird gesetzt, sobald die Platzhalter-Einleitung "@" gefunden wird
for i := 1 to oWords:Count // Anzahl der Wörter
if IstEinPlatzHalter("@"+oWords:Item(i):Text) .and. lHabePlatzHalter
oWords:Item(i):Text := ErsetzePlatzHalter("@"+oWords:Item(i):Text)
oWords:Item(i-1):Text := ""
endif
if oWords:Item(i):Text = "@"
lHabePlatzHalter := .T.
else
lHabePlatzHalter := .F.
endif
next
sleep(200)
oWord:ActiveDocument:Save()
oWord:Quit()
oWord:Destroy()
return nil
Geht ganz schnell und läuft völlig problemlos. Die Performance ist um ein Hundertfaches besser als bei "Suchen und ersetzen" über die entsprechende Word-Funktion.