I need to replace specific placeholder text in a Word document, so I thought using Comate might be a good idea,
The following code should replace the placeholder <date> in a word document with the actual date and save the modified text as new document.
But the code does not work properly: the new document is saved, but the text is not changed..
Any ideas ?
TIA
Oliver
Code: Select all
XIncludeFile "COMatePLUS.pbi"
#wdReplaceAll = 2
Global WordApplication.COMateObject = COMate_CreateObject("Word.Application")
Procedure com_replacetext(WordApplication.COMateObject,sSearch$,sreplace$)
WordApplication\Invoke("Selection\GoTo(1)")
WordApplication\SetProperty("Selection\Find\MatchWholeWord = #false")
WordApplication\SetProperty("Selection\Find\Forward = #True")
WordApplication\SetProperty("Selection\Find\Text = '" + ssearch$ + "'")
WordApplication\Invoke("Selection\Find\Execute(#Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, '" + sreplace$ + "', " + Str(#wdReplaceAll) + ")")
EndProcedure
sTemplateFolder$=GetPathPart(ProgramFilename())
sOutputFolder$=sTemplateFolder$
inputDoc$ =sTemplateFolder$+"input.docx"
outputDoc$ =sOutputFolder$+GetFilePart(inputDoc$,#PB_FileSystem_NoExtension)+"_out."+GetExtensionPart(inputDoc$)
Debug inputDoc$
If WordApplication
If WordApplication\Invoke("Documents\Open('" + inputDoc$ + "')") = #S_OK
com_replacetext(WordApplication,"<DATE>",FormatDate("%dd.%mm.%yyyy",Date()))
WordApplication\Invoke("ActiveDocument\SaveAs('" + outputDoc$ + "')")
WordApplication\Invoke("Quit(0)")
Else
Debug "Couldn't load the document!"
EndIf
WordApplication\Release()
Else
Debug "Couldn't create the application object!"
EndIf