Bulk find and replace

M

mrbanner

i have arond 600 word docs to update they contain links to other word
docs on my webserver.
The the 600 word docs contain links between each other like a big web
of links between them .

I have a csv file that contains the before and after links.
And im looking for a way to do a bulk find and replace where it opens
a file
If the page contains something in the Csv file say A then change it to
B
then repeat till it gets to row 600.
Then opens the next file and does it again

Any suggtions
 
G

Graham Mayor

As we don't know the exact formatting of your CSV. Convert your CSV file to
a Word table of two columns and save as a Word document. You can then use
the following macro to open all the documents in a folder selected from the
macro which will replace the terms in the documents from the first column
with those from the second column. (As links are involved, test it on a
folder containing copies of only a couple of documents to ensure that it
does what you require). Note that it will not work with protected documents!

Option Explicit
Sub BatchReplaceFromTableList()
Dim strFilename As String
Dim strPath As String
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim fDialog As FileDialog
Dim sFname As String

'Change the path in the following line to reflect the name and location of
the table.
sFname = "D:\My Documents\Test\Changes.doc"
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
Set oChanges = Documents.Open(FileName:=sFname, Visible:=True)
Set oTable = oChanges.Tables(1)
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFilename = Dir$(strPath & "*.doc")

While Len(strFilename) <> 0
Set oDoc = Documents.Open(strPath & strFilename)
For i = 1 To oTable.Rows.Count
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
Set rReplacement = oTable.Cell(i, 2).Range
rReplacement.End = rReplacement.End - 1
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Execute findText:=rFindText, _
ReplaceWith:=rReplacement, _
Replace:=wdReplaceAll, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue
End With
End With
Next i
oDoc.Close SaveChanges:=wdSaveChanges
strFilename = Dir$()
Wend
oChanges.Close wdDoNotSaveChanges
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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

Top