The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Microsoft Office

Find and Replace repeatedly over many files-- can it be streamlined?

(4 posts)
  • Started 4 years ago by ggoodman
  • Latest reply from raphoenix
  • Topic Viewed 666 times

Posts: 1


I am a brand-new Member posting my first inquiry. I work in Word 2007 using Windows System 7.

I have a folder containing scores of files, each of which is a blank form letter, all addressed to the same organization. Each letter varies slightly from all others, depending on the situation in which it needs to be used. But all contain the identical street address of the organization it will be e-mailed to. That organization just informed me that its street address has changed slightly-- a new suite number is now used. I know how to use the "Replace" command in any one file at a time. But is there a streamlined way to accomplish this task over every individual file in this folder, so that with just a few clicks I can change the suite number in the address on every blank e-mail form letter? Or am I stuck with having to perform this task file by file, one at a time?

Thanks so much in advance to anyone who can help me.


Posted 4 years ago
Posts: 842

Hi and welcome to HTG

Find and Replace can be a double edged sword under normal circumstance. Do it with a Macro and the dangers multiply exponentially.

Having said that here is a macro to do just that. You MUST create a backup of your files to a different directory

Here is the Macro. You will need to replace the line

fDir = "C:\users\ggoodman\xxxxx\xxxxx\"

with your directory and don't forget the slash (\) at the end

If you get an error message post back with what the message says and which line it stopped on.

Sub ReplaceText()
Resp1 = MsgBox("Press Cancel if you have NOT backed up all your files to a different directory", vbOKCancel)
If Resp1 = vbCancel Then
Exit Sub
End If

Count = 0
Oldnum = InputBox("enter text you want to replace")
NewNum = InputBox("enter text you want to replace it with")

fDir = "C:\users\ggoodman\xxxxx\xxxxx\"
MyFile = Dir(fDir & "*.docx")

Do While MyFile <> ""
Count = Count + 1
Documents.Open FileName:=fDir & MyFile
With Selection.Find
.Text = Oldnum
.Replacement.Text = NewNum
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

MyFile = Dir


MsgBox (Count & " Files accessed")

End Sub

Posted 4 years ago
Posts: 1

Also, you can try Searching in Files with Notepad++

Posted 4 years ago
Posts: 14920


When you see a solution from moreeg, let his post stand without giving further advice.

moreeg is a Real VBA Programmer.

Posted 4 years ago

Topic Closed

This topic has been closed to new replies.