Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Find and Replace Text in Word Document

 Keywords: search replace .doc 

Find/Replace in WORD - quick hack, by Stan Littlefield
wdReplaceAll    = 2
wdReplaceNone   = 0
wdReplaceOne    = 1


file      = StrCat(dirget(),"test.doc")
if !FileExist(file)
	message("Error","File not found")
	exit
endif
oWORD     = ObjectOpen("Word.Application")
oDOCS     = oWORD.Documents
oDOC      = oDOCS.Open(file)
Content   = oDOC.Content
Find      = Content.Find

oldtext   = "duh"
newtext   = "No Duh!"
Find.Execute(:: FindText = oldtext, ReplaceWith = newtext, Replace = wdReplaceAll)

oDOC.SaveAs( StrCat(dirget(),"revisedtest.doc") )
oDOC.Close()
oWORD.Quit()

ObjectClose(Find)
ObjectClose(Content)
ObjectClose(oDOC)
ObjectClose(oDOCS)
ObjectClose(oWORD)
Message("Operation Complete","See the revisedtest.doc file for changes.")
Exit