Is it possible to edit many files at one time?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Problem: We have a website with 100's of pages. On each page, there are
several references to Brand X. In January, Brand X is changing the name to
Brand Y. Each reference on each page needs to be changed.

Question: Is there any way to do this (an add-in or macro) across several
hundred documents? Or is it inevitable that I will have to open each
document and do a "Find and Replace". (Please.....NO!)

Thanks!
 
Amy,

Yes there is a way. The method provided below does a little more than
what you asked for. You will need all of your files in a common
directory. You will need a single word file that contain a two column
table. Row 1 should be headed "Find" and "Replace"

In the second row put Brand X in the left column and Brand Y in the
right column. Save this file to simple location e.g., C:\Find and
replace.doc

Here is the macro code that will find and replace muliple words/phrases
(your case is only one) in multiple files located in a common
directory:

Public Sub BatchFileMultiFindAndReplace()

'This macro is a collection of work by Doug Robbins,
'Peter Hewett, Klaus Linke, Graham Mayor and Greg Maxey

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim rngstory As Word.Range
Dim ListArray
Dim WordList As Document

'Close any documents that may be open
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
'Change the path and filename in the following to suit where
'you have your list of words or phrases
Set WordList = Documents.Open(FileName:="C:\Find and replace.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
WordList.Close
' Get the folder containing the files
With Dialogs(wdDialogCopyFile)
If .Display <> 0 Then
PathToUse = .Directory
Else
MsgBox "Cancelled by User"
Exit Sub
End If
End With
If Left(PathToUse, 1) = Chr(34) Then
PathToUse = Mid(PathToUse, 2, Len(PathToUse) - 2)
End If
myFile = Dir$(PathToUse & "*.*")
While myFile <> ""
'Open each file and make the replacement
Set myDoc = Documents.Open(PathToUse & myFile)
'Fix the skipped blank Header/Footer problem
MakeHFValid
'Iterate through all story types in the current document
For Each rngstory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SearchAndReplaceInStory rngstory, ListArray
'Get next linked story (if any)
Set rngstory = rngstory.NextStoryRange
Loop Until rngstory Is Nothing
Next
'Close the file, saving the changes.
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
End Sub
Public Sub SearchAndReplaceInStory(ByVal rngstory As Word.Range, _
ByRef ListArray As Variant)
'This routine supplied by Peter Hewett and modified by Greg Maxey
Dim Source As Document
Dim i As Integer
Dim Find As Range
Dim Replace As Range
Set Source = ActiveDocument
Source.Activate
For i = LBound(ListArray) To UBound(ListArray) - 1 Step 3
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ListArray(i)
.Replacement.Text = ListArray(i + 1)
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub
Public Sub MakeHFValid()
'And this too
Dim lngJunk As Long
' It does not matter whether we access the Headers or Footers property.
' The critical part is accessing the stories range object
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub

When the File Copy dialog opens, point to the directory containing
your batch of files and select open.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top