FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Word Help
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Word Help
Posted: Wed Jul 25, 2012 11:26 PM

Is there a way to read text from MS-Word?

I have a word doc that contains statements like:

**DAT:xxxxxx

Where xxxxxx will be different every time. I have seen posts where I could do a search for text (in this case **DAT: ) but how would I read the entire statement?

I will be replacing the entire **DAT:xxxxxx with info in my database file.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Re: Word Help
Posted: Wed Jul 25, 2012 11:52 PM
Hi Jeff

Try this -
Thanks to Enrico (EMG) for help with the WORDREPLACE function

Code (fw): Select all Collapse
#include "fivewin.ch"
function doctest(oMainWnd,cPath)
local bDoc,cDoc,aSerial := array(10),i := 0,oWord,oDoc,oSel,bString := {|| ''},;
      cType := 'Ajax',cManu := 'Allect',nNumber := 10000


cDoc := cGetFile32('*.doc')
bDoc := {|| cDoc }
for i := 1 to len(aSerial)
   aSerial[i] := str(nNumber + i) 
next
SysRefresh()


      TRY
        oWord := TOleAuto():New("Word.Application")
        oDoc := oWord:Documents:Open(eval(bDoc))
        oDoc:Select()
        oSel = oWord:Selection
        WORDREPLACE( oSel, "#manu#", cManu)
        WORDREPLACE( oSel, "#type#", cType)
        
        for i := 1 to len(aSerial)
           if i < 10
              bString := {|| "#serial"  + str(i,1) + "#"}
             WORDREPLACE( oSel, eval(bString),aSerial[i])
           else
              bString := {|| "#serial"  + str(i,2) + "#"}
             WORDREPLACE( oSel, eval(bString),aSerial[i])
           endif
        next
        SysRefresh()
        oWord:Visible := TRUE
      CATCH
         MsgAlert('Word May Not Be Installed - Cannot Create Document')
         lWordError := TRUE
      END
return(nil)
//-----------------------------------------------------------------------------------//
STATIC FUNCTION WORDREPLACE( oSel, cSrc, cRpl )
oSel:Start = 0
oSel:End = -1

WHILE oSel:Find:Execute( cSrc )
     oSel:Range:Text = cRpl
ENDDO
RETURN NIL
//------------------------------------------------------------------------------------------------------------------//


Cheers

Colin
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Word Help
Posted: Thu Jul 26, 2012 12:23 AM

Hi Colin,

Thanks for the reply...

Will this let me copy the text to a variable even though I don't know the value of xxxxxx ? (xxxxxx will be a mix of letters, numbers and also include some colons)

I have the WordRepaleAll() working but only if in know what the whole text is to be replaced. In my situation I only know the beginning of the text. The rest will be an unknown but it will be in a fixed format.

It will always start with **DAT: then the next six characters could be anything.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: Word Help
Posted: Thu Jul 26, 2012 06:29 AM
Colin Haig wrote:Thanks to Enrico (EMG) for help with the WORDREPLACE function


This is a better and faster version:

Code (fw): Select all Collapse
#define wdCollapseEnd 0


FUNCTION WORDREPLACEALL( oSel, cSrc, cRpl )

    LOCAL oRng := oSel:Document:Content

    IF AT( cSrc, oRng:Text ) = 0; RETURN .F.; ENDIF

    WHILE oRng:Find:Execute( cSrc )
        oRng:Text = cRpl
        oRng:Collapse( wdCollapseEnd )
    ENDDO

    RETURN .T.


EMG
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Word Help
Posted: Thu Jul 26, 2012 06:17 PM
I figured it out...

To keep things simple I changed the format of the placeholder I am using from **DAT:xxxxxx to <DAT:xxxxxx>

Using Enrico's WorkReplaceAll() function with a slight change I am able to do what I need.

First the Call to the function needs to use some WORD Wildcards "(/<) (DAT:*) (\>)" <---This is the text I am looking for

Code (fw): Select all Collapse
WordReplaceAll( oSel, "(\)(DAT:*)(\>)", "NewText")


Code (fw): Select all Collapse
FUNCTION WordReplaceAll( oSel, cSrc, cRpl )
    LOCAL oRng := oSel:Document:Content
 
    //The following line is needed to tell Word you are using Wildcards
    oRng:Find:MatchWildcards:= .t.

//    I had to remove the following line to make it work.
//    IF AT( cSrc, oRng:Text ) = 0; RETURN .F.; ENDIF   

    WHILE oRng:Find:Execute( cSrc )
        cMyVar := oRng:Text       //Store the found text to a var
        **Do something with the found text here
        oRng:Text = cRpl            //replace the text with cRpl or you can replace with something else
        oRng:Collapse( wdCollapseEnd )
    ENDDO
RETURN .T.
Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)

Continue the discussion