Extract data from MS-Word

Extract data from MS-Word

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

Re: Extract data from MS-Word

Postby anserkk » Wed Jun 08, 2016 4:26 am

Please try this sample

Code: Select all  Expand view
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
User avatar
anserkk
 
Posts: 1329
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Extract data from MS-Word

Postby Jeff Barnes » Wed Jun 08, 2016 3:58 pm

Thanks Anser.
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: Extract data from MS-Word

Postby RAMESHBABU » Thu Jun 09, 2016 3:05 am

Hi, Anser,

Very good example. :)

-Ramesh Babu
User avatar
RAMESHBABU
 
Posts: 614
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: nageswaragunupudi and 90 guests