Winword and OLE

User avatar
driessen
Posts: 1239
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Post by driessen »

Enrico,

Here is the exact source I made :

Code: Select all

******************************************************************************
#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:Document:Content

      IF AT(cSrc,oRng:Text) > 0
         DO WHILE oRng:Find: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:Find: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. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
User avatar
driessen
Posts: 1239
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Post by driessen »

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. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

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
User avatar
driessen
Posts: 1239
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Post by driessen »

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. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

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

EMG
User avatar
driessen
Posts: 1239
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Post by driessen »

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. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

Sorry, I meant "you have erroneously replaced oDoc with oSel":

It is not:

Code: Select all

oSel:StoryRanges[7]
It must be:

Code: Select all

oDoc:StoryRanges[7]
EMG
User avatar
driessen
Posts: 1239
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Post by driessen »

Enrico,

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

Your start of the funcion looks like :

Code: Select all

FUNCTION WORDREPLACERNGALL( oDoc, cSrc, cRpl )
My start of the function looks like :

Code: Select all

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. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

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
User avatar
Marcelo Roggeri
Posts: 275
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina
Contact:

Re: Winword and OLE

Post by Marcelo Roggeri »

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 - FivEdit (Cristobal Navarro)
User avatar
fp
Posts: 76
Joined: Fri Dec 30, 2005 10:25 am
Location: Germany

Re: Winword and OLE

Post by fp »

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
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

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: Select all

oDoc:StoryRanges[ 1 ]:ShapeRange[ i ]:TextFrame:TextRange:Text = "Your text"
or

Code: Select all

oDoc:StoryRanges[ 5 ]:Text = "Your text"
or something similar.

EMG
User avatar
fp
Posts: 76
Joined: Fri Dec 30, 2005 10:25 am
Location: Germany

Re: Winword and OLE

Post by fp »

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:OPEN:STORYRANGES:ITEM:SHAPERANGE:ITEM:TEXTFRAME 0003 DISP_E_MEMBERNOTFOUND: TEXTRANGE

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

Can you help me, please.
Frank-Peter
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Winword and OLE

Post by Enrico Maria Giordano »

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:OPEN:STORYRANGES:ITEM:SHAPERANGE:ITEM:TEXTFRAME 0003 DISP_E_MEMBERNOTFOUND: TEXTRANGE

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

Can you help me, please.
Try this:

Code: Select all

#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
Post Reply