Frequently asked questions for self help.

Knowledge base article 98

How to search and replace in Write

Suppose you had a template and wanted to search and replace a "field holder" with some text of your own. For example, in your document you type *namefld* where you want a user name to appear and *addfld* where the address should appear. The following subroutine implements a search and replace in the active document:

Sub FindAndReplace(find, replace)
Set mydoc = ActiveDocument
stxt = mydoc.Text.Mid(0, mydoc.Text.Count)

i = InStr(stxt, find) - 1
n = Len(find)
If (i > 0) Then
mydoc.Text.Delete i, n
mydoc.Text.Insert i, replace
End If
End Sub

And the following subroutine calls the above sub with some real data:

Sub TestReplace
FindAndReplace "*namefld*", "Phil Roach"
FindAndReplace "*addfld*", "Ability, London, England"
End Sub