Page 2 of 3

Re: Winword and OLE

Posted: Fri Oct 29, 2010 9:48 pm
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.

Re: Winword and OLE

Posted: Sat Oct 30, 2010 9:40 am
by Enrico Maria Giordano
I need of test.doc as well. Send it to my email, please.

EMG

Re: Winword and OLE

Posted: Sat Oct 30, 2010 9:50 am
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.

Re: Winword and OLE

Posted: Sat Oct 30, 2010 12:02 pm
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

Re: Winword and OLE

Posted: Sat Oct 30, 2010 11:31 pm
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.

Re: Winword and OLE

Posted: Sun Oct 31, 2010 10:57 am
by Enrico Maria Giordano
Please review my sample: you have erroneously replaced oWord with oSel.

EMG

Re: Winword and OLE

Posted: Tue Nov 02, 2010 10:20 am
by driessen
Enrico,

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

Thanks.

Re: Winword and OLE

Posted: Tue Nov 02, 2010 10:41 am
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

Re: Winword and OLE

Posted: Tue Nov 02, 2010 2:26 pm
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.

Re: Winword and OLE

Posted: Tue Nov 02, 2010 4:44 pm
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

Re: Winword and OLE

Posted: Wed Oct 03, 2012 2:16 am
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

Re: Winword and OLE

Posted: Fri Jun 07, 2013 8:45 pm
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

Re: Winword and OLE

Posted: Fri Jun 07, 2013 9:54 pm
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

Re: Winword and OLE

Posted: Sat Jun 08, 2013 5:27 am
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.

Re: Winword and OLE

Posted: Sat Jun 08, 2013 9:52 am
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