PDA

View Full Version : Solved: find and replace in all documents



austenr
06-28-2005, 07:18 AM
I need to find a way to find the word "student" and replace all occurances with "client"in all documents inside a folder. Is there a way to do this? Never wrote a Word macro so I could use some help if this can be done. Thanks

austenr
06-28-2005, 07:34 AM
I have this much:

Sub replaceclient()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

End Sub

Now I need help with looping through each document in the folder..

fumei
06-28-2005, 08:14 AM
Itmay be faster, if you have a LOT of folders to work with, to use FileSystemObject.

However, the following will work.
Sub ReplaceText()
Dim myFile
myFile = Dir("c:\test\*.doc")
Do While myFile <> ""
replace_client
myFile = Dir
Loop
End Sub
What this does is, check each file in the folder C:Test, if it has the extension ".doc", it call the sub replace_client. When it is done, it goes and processes the next .doc file.

austenr
06-28-2005, 09:36 AM
Fumei -- I tried to use your code but cant get it to work. Could you please take a look below. Thanks

Sub makechanges()
Dim myFile As Variant
myFile = Dir("C:\Documents and Settings\ahr1267\Desktop\NewFolder\*.doc")
Do While myFile <> ""
replaceclient
myFile = Dir
Loop
End Sub

Sub replaceclient()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With
End Sub

MOS MASTER
06-28-2005, 09:43 AM
That's because the code has to open the documents as well like:
Option Explicit

Sub ReplaceText()
Dim myFile As String

myFile = Dir("c:\test\*.doc")
Do While myFile <> ""
replace_client myFile
myFile = Dir
Loop
End Sub

Sub replace_client(sPath As String)
With Application
.Documents.Open (sPath)

With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

.ActiveDocument.Close True
End With
End Sub


Gerry must have mist that part..

Later..:whistle:

austenr
06-28-2005, 10:36 AM
Does not like the line in bold:

Sub replace_client(sPath As String)
With Application
.Documents.Open (sPath)

With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

.ActiveDocument.Close True
End With
End Sub

can not find test1.doc but it is in the folder named exactly that...:dunno

MOS MASTER
06-28-2005, 10:39 AM
No problems over here what's your Word version?

Run this sub and tell me what the msgbox says:
Sub replace_client(sPath As String)
On Error GoTo strange
With Application
.Documents.Open (sPath)

With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

.ActiveDocument.Close True
End With
strange:
MsgBox Err.Description & vbCr & sPath
Resume Next
End Sub


Later..:whistle:

austenr
06-28-2005, 10:41 AM
2003

MOS MASTER
06-28-2005, 10:42 AM
can not find test1.doc but it is in the folder named exactly that...:dunno
I did notice this one but the file path could be unqualified so please run the new macro. :whistle:

Oh and check are there strange signs in front of the file name like: ~ or $

MOS MASTER
06-28-2005, 10:43 AM
2003
Me to please run the sub the filepath must be wrong some how. :whistle:

austenr
06-28-2005, 10:52 AM
I got it. Evidently it does not like the default file names. Changed the file the macro is in from Doc1.doc to testreplace.doc. Worked fine after that. Solved

fumei
06-28-2005, 10:57 AM
been away...glad that Joost is hovering like a momma bird....

MOS MASTER
06-28-2005, 11:00 AM
I got it. Evidently it does not like the default file names. Changed the file the macro is in from Doc1.doc to testreplace.doc. Worked fine after that. Solved
The explanation seams really strange to me. Please do try my latest code and let me see what the msgbox produces.

Go to windows explorer first and turn of hide extensions for known file types to see if you don't have any double extensions..

I'm really glad it's working for you but the logic of default file names doesn't make sence!

MOS MASTER
06-28-2005, 11:02 AM
been away...glad that Joost is hovering like a momma bird....
Well with you arround I could hover to ...... :goofball:

Really Austen change back the name to Doc1.doc

And try this new more sollid code:
Const sRoot As String = "C:\Documents and Settings\Joost Verdaasdonk\Bureaublad\testing\"
Sub ReplaceText()
Dim myFile As String

myFile = Dir(sRoot & "*.doc")
Do While myFile <> ""
replace_client myFile
myFile = Dir
Loop
End Sub
Sub replace_client(sPath As String)
Dim oDoc As Word.Document
With Application
Set oDoc = .Documents.Open(sRoot & sPath)

With oDoc.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With

oDoc.Close True
End With

Set oDoc = Nothing
End Sub

Change the path of sRoot to the one your using it should work now. :whistle:

austenr
06-28-2005, 11:15 AM
works fine. Thanks to you both.

MOS MASTER
06-28-2005, 11:16 AM
works fine. Thanks to you both.
Ok now I can go and eat something..your welcome! :yes

austenr
06-28-2005, 12:26 PM
Another question. I need to put a prompt for the text to find and a prompt for the text to replace. Where is the best place to put that so it does not pop up for every document that is opened?

MOS MASTER
06-28-2005, 12:39 PM
Like:
Option Explicit
Const sRoot As String = "C:\Documents and Settings\Joost Verdaasdonk\Bureaublad\testing\"
Sub ReplaceText()
Dim sSearch As String
Dim sReplace As String
Dim myFile As String

sSearch = InputBox("Which word are you looking for")
sReplace = InputBox("The replacement word please")

myFile = Dir(sRoot & "*.doc")
Do While myFile <> ""
replace_client myFile, sSearch, sReplace
myFile = Dir
Loop
End Sub
Sub replace_client(sPath As String, sSearch As String, sReplace As String)
Dim oDoc As Word.Document
With Application
Set oDoc = .Documents.Open(sRoot & sPath)

With oDoc.Content.Find
.ClearFormatting
.Text = sSearch
With .Replacement
.ClearFormatting
.Text = sReplace
End With
.Execute Replace:=wdReplaceAll
End With

oDoc.Close True
End With

Set oDoc = Nothing
End Sub

Enjoy! :whistle:

austenr
06-28-2005, 02:06 PM
I was using a Prompt in the first sub. What I did not sue was define them in the second sub). :banghead: Thanks

MOS MASTER
06-28-2005, 02:10 PM
I was using a Prompt in the first sub. What I did not sue was define them in the second sub). :banghead: Thanks

Well defining them isn't enough..you have to pass them! :rofl:

Again your most welcome..please do visit us again in the wonderful world of Word! :hi:

austenr
06-28-2005, 02:12 PM
Well..Word is not my most favorite app anyway..

MOS MASTER
06-28-2005, 02:13 PM
Well..Word is not my most favorite app anyway..
What! :devil: :rotlaugh:

Your favorite app is Excel! :whistle: (according to your post that is)