Word Help

Word Help

Postby Jeff Barnes » 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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Word Help

Postby Colin Haig » Wed Jul 25, 2012 11:52 pm

Hi Jeff

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

Code: Select all  Expand view

#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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Word Help

Postby Jeff Barnes » 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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Word Help

Postby Enrico Maria Giordano » Thu Jul 26, 2012 6:29 am

Colin Haig wrote:Thanks to Enrico (EMG) for help with the WORDREPLACE function


This is a better and faster version:

Code: Select all  Expand view
#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
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Word Help

Postby Jeff Barnes » Thu Jul 26, 2012 6: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: Select all  Expand view
WordReplaceAll( oSel, "(\)(DAT:*)(\>)", "NewText")


Code: Select all  Expand view
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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 52 guests