Alaska Software Inc. - ActiveX Word - replace a text
Username: Password:
AuthorTopic: ActiveX Word - replace a text
Domingo SerranoActiveX Word - replace a text
on Tue, 12 Apr 2016 18:57:24 +0200
Hello,
I only need to replace a text (into the entire document) with another.
i.e.:  replace "original text" with "final text".
Thanks in advance.

Domingo Serrano
Domingo Serranonotifications
on Tue, 12 Apr 2016 18:58:00 +0200
César Calvo Re: ActiveX Word - replace a text
on Fri, 15 Apr 2016 22:53:31 +0200
Hello Domingo.
Prove with this source code:

#include "activex.ch"

//////////////////////////////////////////////////////////////////////
FUNCTION ReplaceBookmark(oBM,cBM,cValue)
  LOCAL lRet := oBM:Exists(cBM)
  LOCAL oF


  IF(lRet)
    ? "Replace", cBM, "with", cValue
    oF := oBM:Item(cBM)


    oF:Range:Font:Bold  := 1

    oF:Range:Text := cValue
    oF:Destroy()
  ENDIF
RETURN(lRet)


//////////////////////////////////////////////////////////////////////
 Open a MS Word document and replace the bookmarks with the values
 passed in the array aData. The resulting document is then saved
 back to the hard drive.
//////////////////////////////////////////////////////////////////////
FUNCTION WordFillDocument(cFile,aData,cSaveAs,lPrint)
  LOCAL oWord,oBM,oDoc

   Create a Word ActiveX component
  oWord := CreateObject("Word.Application")
  IF Empty( oWord )
    MsgBox( "Microsoft Word is not installed" )
  ENDIF

  oWord:visible := .T.

   Open a Word document and retrieve the bookmarks
   collection.
  oWord:documents:open( cFile )
  oDoc := oWord:ActiveDocument
  oBM  := oDoc:Bookmarks

   Replace the Bookmark with a new value
  ReplaceBookmark(oBM , "NAME" , aData[1] )
  ReplaceBookmark(oBM , "TO"   , aData[2] )
  ReplaceBookmark(oBM , "EUR"  , aData[3] )

   Save the resulting Word document
  IF(ValType(cSaveAs)=="C")
    oDoc:saveas(cSaveAs)
  ENDIF

   Optional print out of document to standard
   printer
  IF(ValType(lPrint)=="L" .AND. lPrint)
    oDoc:PrintOut()
  ENDIF

   Close the document and destroy the ActiveX
   object
  oDoc:close()
  oWord:Quit()
  oWord:destroy()
RETURN NIL


//////////////////////////////////////////////////////////////////////
 Main program entry point of the application
//////////////////////////////////////////////////////////////////////
PROCEDURE main
  LOCAL cDir,cFile, cSave
  LOCAL aData := {}

  AAdd( aData, "in scadenza" )
  AAdd( aData, "Albo Professionali degli Spedizionieri" )
  AAdd( aData, "Euro 450,00" )

   Determine fully-qualified path for
   loading the word template document
  cDir := CurDrive()+":\"+CurDir()
  cFile := cDir +"\Template.dot"
  cSave := cDir+"\Saved.doc"

   Helper function:
   - loads existing word document
   - uses the array aData to feed in data
     into bookmarks
   - saves the word document under a new
     name
  WordFillDocument(cFile,aData,cSave,.F.)

  ? "Document", cSave, "created"
  WAIT

RETURN

"Domingo Serrano" escribió en el mensaje de 
noticias:44596e6$3a06f80e$344ab@news.alaska-software.com...

Hello,
I only need to replace a text (into the entire document) with another.
i.e.:  replace "original text" with "final text".
Thanks in advance.

Domingo Serrano


Template.dot
Domingo SerranoRe: ActiveX Word - replace a text
on Sat, 16 Apr 2016 02:51:16 +0200
Thank you very much, César !!.
The code is marvellous.
It is just what I needed.
Best regards.

Domingo Serrano.


César Calvo <ccalvoc@telefonica.net> wrote in message
news:3237b935$2531ab49$529c1@news.alaska-software.com...
>Hello Domingo.
>Prove with this source code:
>
>#include "activex.ch"
>
>//////////////////////////////////////////////////////////////////////
>FUNCTION ReplaceBookmark(oBM,cBM,cValue)
>  LOCAL lRet := oBM:Exists(cBM)
>  LOCAL oF
>
>
>  IF(lRet)
>    ? "Replace", cBM, "with", cValue
>    oF := oBM:Item(cBM)
>
>
>    oF:Range:Font:Bold  := 1
>
>    oF:Range:Text := cValue
>    oF:Destroy()
>  ENDIF
>RETURN(lRet)
>
>
>//////////////////////////////////////////////////////////////////////
>// Open a MS Word document and replace the bookmarks with the values
>// passed in the array aData. The resulting document is then saved
>// back to the hard drive.
>//////////////////////////////////////////////////////////////////////
>FUNCTION WordFillDocument(cFile,aData,cSaveAs,lPrint)
>  LOCAL oWord,oBM,oDoc
>
>   Create a Word ActiveX component
>  oWord := CreateObject("Word.Application")
>  IF Empty( oWord )
>    MsgBox( "Microsoft Word is not installed" )
>  ENDIF
>
>  oWord:visible := .T.
>
>   Open a Word document and retrieve the bookmarks
>   collection.
>  oWord:documents:open( cFile )
>  oDoc := oWord:ActiveDocument
>  oBM  := oDoc:Bookmarks
>
>   Replace the Bookmark with a new value
>  ReplaceBookmark(oBM , "NAME" , aData[1] )
>  ReplaceBookmark(oBM , "TO"   , aData[2] )
>  ReplaceBookmark(oBM , "EUR"  , aData[3] )
>
>   Save the resulting Word document
>  IF(ValType(cSaveAs)=="C")
>    oDoc:saveas(cSaveAs)
>  ENDIF
>
>   Optional print out of document to standard
>   printer
>  IF(ValType(lPrint)=="L" .AND. lPrint)
>    oDoc:PrintOut()
>  ENDIF
>
>   Close the document and destroy the ActiveX
>   object
>  oDoc:close()
>  oWord:Quit()
>  oWord:destroy()
>RETURN NIL
>
>
>//////////////////////////////////////////////////////////////////////
>// Main program entry point of the application
>//////////////////////////////////////////////////////////////////////
>PROCEDURE main
>  LOCAL cDir,cFile, cSave
>  LOCAL aData := {}
>
>  AAdd( aData, "in scadenza" )
>  AAdd( aData, "Albo Professionali degli Spedizionieri" )
>  AAdd( aData, "Euro 450,00" )
>
>   Determine fully-qualified path for
>   loading the word template document
>  cDir := CurDrive()+":\"+CurDir()
>  cFile := cDir +"\Template.dot"
>  cSave := cDir+"\Saved.doc"
>
>   Helper function:
>   - loads existing word document
>   - uses the array aData to feed in data
>     into bookmarks
>   - saves the word document under a new
>     name
>  WordFillDocument(cFile,aData,cSave,.F.)
>
>  ? "Document", cSave, "created"
>  WAIT
>
>RETURN
>
>"Domingo Serrano" escribió en el mensaje de 
>noticias:44596e6$3a06f80e$344ab@news.alaska-software.com...
>
>Hello,
>I only need to replace a text (into the entire document) with another.
>i.e.:  replace "original text" with "final text".
>Thanks in advance.
>
>Domingo Serrano
Hubert BrandelRe: ActiveX Word - replace a text
on Wed, 22 Nov 2017 10:37:53 +0100
Am 12.04.2016 um 18:57 schrieb Domingo Serrano:
> Hello,
> I only need to replace a text (into the entire document) with another.
> i.e.:  replace "original text" with "final text".
> Thanks in advance.
> 
> Domingo Serrano
> 

Maybe you got it, but maybe some other do have the same question:

#include "activex.ch"
#include "word2010.ch"


//////////////////////////////////////////////////////////////////////
 Main()-Prozedur der Anwendung
//////////////////////////////////////////////////////////////////////
PROCEDURE main
   LOCAL cDir,cFile, cSaveAs
   LOCAL oWord,oDoc, aReplace

   set charset to ansi

   aReplace := { { "from text"         , "to replace text" }  ,;
                 { "..."  , "..." } }

   set alternate to Ersetzen-LOG.TXT
   set alternate on

   cDir    := CurDrive()+":\"+CurDir()
   cFile   := cDir+"\Test.docx"
   cSaveAs := cDir+"\Test1.docx"

    Erzeugen einer ActiveX-Komponente
   oWord := CreateObject("Word.Application")
   IF Empty( oWord )
     MsgBox( "Microsoft Word ist nicht installiert" )
   ENDIF

   oWord:visible := .T.

    Oeffnen eines Word-Dokuments
   oWord:documents:open( cFile )
   oDoc := oWord:ActiveDocument

    word replace ...
   aEval( aReplace, {|aR| WordReplace(oWord,aR[1],aR[2]) } )

    Speichern des Ergebnisses
   IF(ValType(cSaveAs)=="C")
     oDoc:saveas(cSaveAs)
   ENDIF


    Schliessen des Dokuments und zerstoeren das
    ActiveX-Objektes.
   oDoc:close()
   oWord:Quit()
   oWord:destroy()

   ? "Dokument", cSaveAs, "erzeugt"

   set alternate to



   inkey(20)

RETURN

function WordReplace(oWord,cVon,cNach)
    local oSel, lOK
   oSel := oWord:selection
   oSel:Find:ClearFormatting()
   oSel:Find:Replacement:ClearFormatting()

   lOK := oSel:Find:Execute(  cVon,;    FindText
                               .f.,;    MatchCase
                               .f.,;    MatchWholeWord
                               .f.,;    MatchWildcards
                               .f.,;    MatchSoundsLike
                               .f.,;    MatchAllWordForms
                               .t.,;    Forward
                    wdFindContinue,;    Wrap
                               .f.,;    Format
                             cNach,;    ReplaceWith
                      wdReplaceAll,;    Replace
                               .f.,;    MatchKashida
                               .f.,;    MatchDiacritics
                               .f.,;    MatchAlefHamza
                               .f.  )   MatchControl

   ? "ersetze",cVon,cNach, lOK

return lOK