ComatePlus: Replace in Word does not work

Just starting out? Need help? Post your questions and find answers here.
Oliver13
User
User
Posts: 90
Joined: Thu Sep 30, 2010 6:40 am

ComatePlus: Replace in Word does not work

Post by Oliver13 »

Hi all,

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
User avatar
spikey
Enthusiast
Enthusiast
Posts: 761
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: ComatePlus: Replace in Word does not work

Post by spikey »

If the file is created you have a working COM object so, at a guess, one of the 'Find' related instructions is returning an error. However I can't test to find out because I don't have access to MS Office any more. Replace your procedure with this version which will output some debug information, it should give you a better idea of what's going wrong or, at least, at which point it's going wrong:

Code: Select all

Procedure com_replacetext(WordApplication.COMateObject,sSearch$,sreplace$)
  
  WordApplication\Invoke("Selection\GoTo(1)")
  Debug COMate_GetLastErrorDescription() 
  
  WordApplication\SetProperty("Selection\Find\MatchWholeWord = #false")
  Debug COMate_GetLastErrorDescription() 
  
  WordApplication\SetProperty("Selection\Find\Forward = #True")
  Debug COMate_GetLastErrorDescription() 
  
  WordApplication\SetProperty("Selection\Find\Text = '" + ssearch$ + "'")
  Debug COMate_GetLastErrorDescription() 
  
  WordApplication\Invoke("Selection\Find\Execute(#Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, '" + sreplace$ + "', " + Str(#wdReplaceAll) + ")")
  Debug COMate_GetLastErrorDescription() 
  
EndProcedure
Post Reply