possible? or asking too much :-)
Thanks
ChrisPr
find and replace in multiple docs...
-
- Lounger
- Posts: 34
- Joined: 25 Jan 2010, 09:20
- Location: erewhon, Cambridgeshire, UK
-
- Administrator
- Posts: 78530
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: find and replace in multiple docs...
If you want to replace text in all documents in a folder, you can do so using a VBA macro, for example:
You could use InputBox to ask the user for the 'Find what' and 'Replace with' text instead of using fixed strings.
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
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!
Reason: to correct error in code - thanks to ABabeNChrist for pointing it out!
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: find and replace in multiple docs...
Hi Hans
shouldnt this line
be
shouldnt this line
Code: Select all
.Execute FindText:="abc", ReplaceWith:="xyz", Replace:=wdReplaceAll\
Code: Select all
.Execute FindText:="abc", ReplaceWith:="xyz", Replace:=wdReplaceAll
-
- Administrator
- Posts: 78530
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: find and replace in multiple docs...
Yes, of course. Thank you! (That's what you get if you write air code...)
Best wishes,
Hans
Hans
-
- SilverLounger
- Posts: 1868
- Joined: 25 Jan 2010, 14:00
- Location: Conroe, Texas
Re: find and replace in multiple docs...
I get lucky every now and then
its been your great teaching skills that has helped me
a few months ago I wouldnt of had a clue
its been your great teaching skills that has helped me
a few months ago I wouldnt of had a clue
-
- Administrator
- Posts: 78530
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- Administrator
- Posts: 78530
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: find and replace in multiple docs...
For a more flexible solution, you could take a look at Greg Maxey's free add-in VBA Find and Replace.
Best wishes,
Hans
Hans