FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Winword and OLE
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Winword and OLE
Posted: Fri Oct 29, 2010 09:48 PM
Enrico,

Here is the exact source I made :
Code (fw): Select all Collapse
******************************************************************************
#INCLUDE "FIVEWIN.CH"
#DEFINE DEFAULT(xVar,xVal) xVar=IF(xVar=NIL,xVal,xVar)

#DEFINE wdCollapseEnd 0
******************************************************************************

FUNCTION MAIN(cInput)

    LOCAL cDisk := DiskName()
    LOCAL cDir  := CurDir()

    LOCAL cTel  := 0

    DEFAULT(cInput,"1")

   PRIVATE oWord,oDoc,oSel

   TRY
      oWord := TOleAuto():New("Word.Application")
*     oWORD := CreateObject("word.Application")
      oWord:Run("JUD_CHECK")
   CATCH
      MSGSTOP("Word is niet op deze computer geïnstalleerd !!!")
      RETURN(.T.)
   END

   IF FILE(ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC")

    IF VAL(cInput) = 1 .OR. VAL(cInput) = 2

         oDoc := oWord:Documents:Open(ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC")
         oDoc:Select()
         oSel := oWord:Selection

         cTel := WORDREPLACE(oSel,"AAAAA","BBBBB",VAL(cInput))

*        oDoc:PrintOut()
         oDoc:Save()
         oDoc:Close(0)

         MsgAlert("De tekstvervanging werd uitgevoerd !!!" + CHR(13) + CHR(13) + "Er werden " + ALLTRIM(STR(cTel,10,0)) + " vervangingen uitgevoerd.","Opgelet")

      ELSE

        MsgAlert("U heeft een foutieve input opgegeven !!!","Opgelet")

      ENDIF

    ELSE

        MsgAlert("Het bestand " + ALLTRIM(cDisk)+":\"+ALLTRIM(cDir)+"\TEST.DOC" + " bestaat niet","Opgelet")

    ENDIF

   oWord:Quit()

   Quit

RETURN(.T.)

******************************************************************************

FUNCTION WORDREPLACE(oSel,cSrc,cRpl,cSrt)

   LOCAL lOk  := .F.
   LOCAL lTel := 0

   LOCAL oRng

   DEFAULT(cSrt,1)
   
   MsgInfo("OK1")

*  cSrt = 1 : tekst wordt overal vervangen, ook in header en footer
*  cSrt = 2 : tekst wordt overal vervangen, maar niet in header en footer

   IF cSrt = 1

      oRng := oSel:Content

      IF AT(cSrc,oRng:Text) > 0
         DO WHILE oRng:Execute(cSrc)
            oRng:Text = cRpl
            oRng:Collapse(wdCollapseEnd)
            lTel++
         ENDDO
      ENDIF

   ELSEIF cSrt = 2

      oRng = oSel:StoryRanges[7]

      IF AT(cSrc,oRng:Text) > 0
         DO WHILE oRng:Execute(cSrc)
            oRng:Text := cRpl
            oRng      := oSel:StoryRanges[7]
            lTel++
         ENDDO
         lOk = .T.
      ENDIF

   ENDIF

    MsgInfo("OK2")

RETURN(lTel)

******************************************************************************


Thank you very much in advance.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Sat Oct 30, 2010 09:40 AM

I need of test.doc as well. Send it to my email, please.

EMG

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Winword and OLE
Posted: Sat Oct 30, 2010 09:50 AM
Enrico,

Since I don't have your e-mail, I made it possible to download both files :

http://www.ma-consult.be/testword.prg
http://www.ma-consult.be/test.doc

Thanks a lot.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Sat Oct 30, 2010 12:02 PM

Thank you. Your sample prints

OK1
OK2
De tekstvervanging...

Is it correct? I don't know. Please take the time to prepare a reduced sample of the problem.

EMG

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Winword and OLE
Posted: Sat Oct 30, 2010 11:31 PM
Enrico,

I made 2 possibilities available in my example.

TESTWORD.EXE 1 : the replace is done but not in the header and the footer (like the original example in this toppic)

TESTWORD.EXE 2 : the replace is done, also in the header and footer.

It is in the second case that the error occurs. The replacement in the first example runs fine.

Thanks.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Sun Oct 31, 2010 10:57 AM

Please review my sample: you have erroneously replaced oWord with oSel.

EMG

Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Winword and OLE
Posted: Tue Nov 02, 2010 10:20 AM

Enrico,

I'm very sorry but I don't see where the error is. But that must be my mistake.

Thanks.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Tue Nov 02, 2010 10:41 AM
Sorry, I meant "you have erroneously replaced oDoc with oSel":

It is not:

Code (fw): Select all Collapse
oSel:StoryRanges[7]


It must be:

Code (fw): Select all Collapse
oDoc:StoryRanges[7]


EMG
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Winword and OLE
Posted: Tue Nov 02, 2010 02:26 PM
Enrico,

I'm very sorry but I don't see what is wrong.

Your start of the funcion looks like :
Code (fw): Select all Collapse
FUNCTION WORDREPLACERNGALL( oDoc, cSrc, cRpl )


My start of the function looks like :
Code (fw): Select all Collapse
FUNCTION WORDREPLACE(oSel,cSrc,cRpl,cSrt)


So my "oSel" = your "oDoc". I use "oSel" since "oWord" has already been used in the main section.

So I don't see any difference.

Sorry.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Tue Nov 02, 2010 04:44 PM

You have to pass oDoc too to WORDREPLACE() function so you can use it in the expression oDoc:StoryRanges[7] that you erroneously wrote as oSel:StoryRanges[7]. Please note that oSel is not equal to oDoc.

EMG

Posts: 344
Joined: Sat Jul 22, 2006 09:04 PM
Re: Winword and OLE
Posted: Wed Oct 03, 2012 02:16 AM

Hola buenas noches, alguien tiene esos dos archivos? el de word y el prg? ya que caduco el link el que figura ahi.
Gracias
Marcelo

FWH - Harbour - BCC7 - PellesC
Posts: 76
Joined: Fri Dec 30, 2005 10:25 AM
Re: Winword and OLE
Posted: Fri Jun 07, 2013 08:45 PM
I want to replace adress data in a frame of a Winword letter.

"Wordreplace" unfortunately doesn't replace words in a frame.

I have in my Word-document a frame for the address data, e.g.

@Name
@Street
etc.

How can I get the content of the frame to replace it?

Thanks
Frank-Peter
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Fri Jun 07, 2013 09:54 PM
Frank,

fp wrote:I want to replace adress data in a frame of a Winword letter.

"Wordreplace" unfortunately doesn't replace words in a frame.

I have in my Word-document a frame for the address data, e.g.

@Name
@Street
etc.

How can I get the content of the frame to replace it?

Thanks


Try

Code (fw): Select all Collapse
oDoc:StoryRanges[ 1 ]:ShapeRange[ i ]:TextFrame:TextRange:Text = "Your text"


or

Code (fw): Select all Collapse
oDoc:StoryRanges[ 5 ]:Text = "Your text"


or something similar.

EMG
Posts: 76
Joined: Fri Dec 30, 2005 10:25 AM
Re: Winword and OLE
Posted: Sat Jun 08, 2013 05:27 AM
Thank you Enrico,

but

'oDoc:StoryRanges[ 5 ]:Text'

allways returns the content of the first frame (I still have other frames in my letter)

and

oDoc:StoryRanges[ 1 ]:ShapeRange[ i ]:TextFrame:TextRange:Text

get an error

Word.Application:DOCUMENTS:STORYRANGES:SHAPERANGE:TEXTFRAME 0003 DISP_E_MEMBERNOTFOUND: TEXTRANGE

(I had testet it with i = 1 to i =10)

Can you help me, please.
Frank-Peter
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Winword and OLE
Posted: Sat Jun 08, 2013 09:52 AM
Frank,

fp wrote:Thank you Enrico,

but

'oDoc:StoryRanges[ 5 ]:Text'

allways returns the content of the first frame (I still have other frames in my letter)

and

oDoc:StoryRanges[ 1 ]:ShapeRange[ i ]:TextFrame:TextRange:Text

get an error

Word.Application:DOCUMENTS:STORYRANGES:SHAPERANGE:TEXTFRAME 0003 DISP_E_MEMBERNOTFOUND: TEXTRANGE

(I had testet it with i = 1 to i =10)

Can you help me, please.


Try this:

Code (fw): Select all Collapse
#define wdTextFrameStory 5


FUNCTION WORDREPLACERNGALL( oDoc, cSrc, cRpl )

    LOCAL lOk := .F.

    LOCAL oRng

    TRY
        oRng = oDoc:StoryRanges[ wdTextFrameStory ]

        IF AT( cSrc, oRng:Text ) > 0
            WHILE oRng:Find:Execute( cSrc )
                oRng:Text = cRpl
                oRng = oDoc:StoryRanges[ wdTextFrameStory ]
            ENDDO

            lOk = .T.
        ENDIF
    CATCH
    END

    RETURN lOk


EMG