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>
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.
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: nageswaragunupudi and 90 guests