FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Extract data from MS-Word
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Extract data from MS-Word
Posted: Tue Jun 07, 2016 11:13 AM

Is there a way to extract data from MS-Word that is between text,

Example:

In my word document (.doc or .docx) I would like to assign a var to the data found between <DATA> and </DATA>
<DATA>Some text</DATA>

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: Extract data from MS-Word
Posted: Wed Jun 08, 2016 04:26 AM
Please try this sample

Code (fw): Select all Collapse
Function Main()
   Local oWord,oDoc,oSel,oActiveDoc,aSrchReplace, cTemplateFile, cTemplateSaveAs
   
   cTemplateFile:= "D:\MyFolder\MyWordFile.Doc"
   cTemplateSaveAs:="D:\MyFolder\MyWordFile2.Doc"
      
   TRY
        oWord := CreateObject("Word.Application")
    CATCH
        MsgInfo("Word is not installed in this PC. Unable to continue further")
        Return NIL
    END

    TRY
        oDoc := oWord:Documents:Open(cTemplateFile)  
    CATCH
        MsgInfo("Unable to open the template file "+cTemplateFile)
        oWord:Quit()
        Return NIL        
    END
    oWord:Visible:=.T.
    oDoc:Select()
    oSel = oWord:Selection
    
    aSrchReplace:={ {"<DATA>agreement_day</DATA>"       , "01"                  }, ;
                    {"<DATA>agreement_monthyear</DATA>" , "June, 2016"          }, ;
                    {"<DATA>Customer_Name</DATA>"       , "Mr.John"             }, ;    
                    
    For j:=1 to Len(aSrchReplace)
        WordReplace( oSel, aSrchReplace[j][1], aSrchReplace[j][2] )                  
    Next j
    oActiveDoc:=oWord:Get("ActiveDocument")
    oActiveDoc:SaveAs(cTemplateSaveAs)
    oWord:Documents:Close()  
    oWord:Quit()    
    
Return
    
    
//-------------------------------------------------------------------------------------------------------------------//
FUNCTION WordReplace( oSel, cSrc, cRpl )

    Local wdCollapseEnd:=0

    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.


You may modify the above given sample to replace <DATA>Some text</DATA> as per your requirement.

Regards
Anser
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Extract data from MS-Word
Posted: Wed Jun 08, 2016 03:58 PM

Thanks Anser.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: Extract data from MS-Word
Posted: Thu Jun 09, 2016 03:05 AM

Hi, Anser,

Very good example. :)

-Ramesh Babu

Continue the discussion