find and replace in multiple docs...

ChrisPr
Lounger
Posts: 34
Joined: 25 Jan 2010, 09:20
Location: erewhon, Cambridgeshire, UK

find and replace in multiple docs...

Post by ChrisPr »

possible? or asking too much :-)

Thanks
ChrisPr

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find and replace in multiple docs...

Post by HansV »

If you want to replace text in all documents in a folder, you can do so using a VBA macro, for example:

Code: Select all

Sub ProcessFiles()
  ' Edit as needed, but keep trailing backslash
  Const strPath = "C:\Word\"
  Dim strFile As String
  Dim doc As Document

  On Error GoTo ErrHandler

  strFile = Dir(strPath & "*.doc")
  Do While Not strFile = ""
    Set doc = Documents.Open(FileName:=strPath & strFile, AddToRecentFiles:=False)
    With doc.Content.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      ' Change the text as needed
      .Execute FindText:="abc", ReplaceWith:="xyz", Replace:=wdReplaceAll
    End With
    doc.Close SaveChanges:=wdSaveChanges
    strFile = Dir
  Loop

ExitHandler:
  Set doc = Nothing
  Exit Sub

ErrHandler:
  MsgBox Err.Description, vbExclamation
  Resume ExitHandler
End Sub
You could use InputBox to ask the user for the 'Find what' and 'Replace with' text instead of using fixed strings.
Last edited by HansV on 14 Jun 2010, 13:47, edited 1 time in total.
Reason: to correct error in code - thanks to ABabeNChrist for pointing it out!
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: find and replace in multiple docs...

Post by ABabeNChrist »

Hi Hans
shouldnt this line

Code: Select all

 .Execute FindText:="abc", ReplaceWith:="xyz", Replace:=wdReplaceAll\
be

Code: Select all

 .Execute FindText:="abc", ReplaceWith:="xyz", Replace:=wdReplaceAll

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find and replace in multiple docs...

Post by HansV »

Yes, of course. Thank you! (That's what you get if you write air code...)
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: find and replace in multiple docs...

Post by ABabeNChrist »

I get lucky every now and then
its been your great teaching skills that has helped me :cheers:
a few months ago I wouldnt of had a clue :scratch:

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find and replace in multiple docs...

Post by HansV »

Thanks for your kind words!
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: find and replace in multiple docs...

Post by HansV »

For a more flexible solution, you could take a look at Greg Maxey's free add-in VBA Find and Replace.
Best wishes,
Hans