Winword and OLE

Re: Winword and OLE

Postby driessen » Fri Oct 29, 2010 9:48 pm

Enrico,

Here is the exact source I made :
Code: Select all  Expand view
******************************************************************************
#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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Postby Enrico Maria Giordano » Sat Oct 30, 2010 9:40 am

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Winword and OLE

Postby driessen » Sat Oct 30, 2010 9: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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

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

Re: Winword and OLE

Postby driessen » 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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Postby Enrico Maria Giordano » Sun Oct 31, 2010 10:57 am

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

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Winword and OLE

Postby driessen » 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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Postby Enrico Maria Giordano » Tue Nov 02, 2010 10:41 am

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

It is not:

Code: Select all  Expand view
oSel:StoryRanges[7]


It must be:

Code: Select all  Expand view
oDoc:StoryRanges[7]


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Winword and OLE

Postby driessen » Tue Nov 02, 2010 2:26 pm

Enrico,

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

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


My start of the function looks like :
Code: Select all  Expand view
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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Winword and OLE

Postby Enrico Maria Giordano » Tue Nov 02, 2010 4: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
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Winword and OLE

Postby Marcelo Roggeri » Wed Oct 03, 2012 2: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
User avatar
Marcelo Roggeri
 
Posts: 342
Joined: Sat Jul 22, 2006 9:04 pm
Location: Venado Tuerto - Santa Fe -Argentina

Re: Winword and OLE

Postby fp » Fri Jun 07, 2013 8: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
User avatar
fp
 
Posts: 76
Joined: Fri Dec 30, 2005 10:25 am
Location: Germany

Re: Winword and OLE

Postby Enrico Maria Giordano » Fri Jun 07, 2013 9: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: Select all  Expand view
oDoc:StoryRanges[ 1 ]:ShapeRange[ i ]:TextFrame:TextRange:Text = "Your text"


or

Code: Select all  Expand view
oDoc:StoryRanges[ 5 ]:Text = "Your text"


or something similar.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Winword and OLE

Postby fp » Sat Jun 08, 2013 5: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: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
fp
 
Posts: 76
Joined: Fri Dec 30, 2005 10:25 am
Location: Germany

Re: Winword and OLE

Postby Enrico Maria Giordano » Sat Jun 08, 2013 9: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: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  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

PreviousNext

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 100 guests