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