WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

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

Article ID:   W15663
File Created: 2003:05:13:11:29:24
Last Updated: 2003:05:13:11:29:24