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
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
Peace of mind is found in some of the strangest places.
I have this much:
[VBA]Sub replaceclient()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "Student"
With .Replacement
.ClearFormatting
.Text = "Client"
End With
.Execute Replace:=wdReplaceAll
End With
End Sub[/VBA]
Now I need help with looping through each document in the folder..
Peace of mind is found in some of the strangest places.
Itmay be faster, if you have a LOT of folders to work with, to use FileSystemObject.
However, the following will work.
[vba]Sub ReplaceText()
Dim myFile
myFile = Dir("c:\test\*.doc")
Do While myFile <> ""
replace_client
myFile = Dir
Loop
End Sub[/vba]
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.
Fumei -- I tried to use your code but cant get it to work. Could you please take a look below. Thanks
[VBA] 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
[/VBA]
Peace of mind is found in some of the strangest places.
That's because the code has to open the documents as well like:[VBA]
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
[/VBA]
Gerry must have mist that part..
Later..
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Does not like the line in bold:
[VBA] 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 [/VBA]
can not find test1.doc but it is in the folder named exactly that...
Peace of mind is found in some of the strangest places.
No problems over here what's your Word version?
Run this sub and tell me what the msgbox says:[VBA]
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
[/VBA]
Later..
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
2003
Peace of mind is found in some of the strangest places.
I did notice this one but the file path could be unqualified so please run the new macro.Originally Posted by austenr
Oh and check are there strange signs in front of the file name like: ~ or $
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Me to please run the sub the filepath must be wrong some how.Originally Posted by austenr
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
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
Peace of mind is found in some of the strangest places.
been away...glad that Joost is hovering like a momma bird....
The explanation seams really strange to me. Please do try my latest code and let me see what the msgbox produces.Originally Posted by austenr
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!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
Well with you arround I could hover to ......Originally Posted by fumei
Really Austen change back the name to Doc1.doc
And try this new more sollid code:[VBA]
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
[/VBA]
Change the path of sRoot to the one your using it should work now.
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
works fine. Thanks to you both.
Peace of mind is found in some of the strangest places.
Ok now I can go and eat something..your welcome!Originally Posted by austenr
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
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?
Peace of mind is found in some of the strangest places.
Like:[VBA]
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
[/VBA]
Enjoy!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)
I was using a Prompt in the first sub. What I did not sue was define them in the second sub). Thanks
Peace of mind is found in some of the strangest places.
Well defining them isn't enough..you have to pass them!Originally Posted by austenr
Again your most welcome..please do visit us again in the wonderful world of Word!
_________
Groetjes,
Joost Verdaasdonk
M.O.S. Master
Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
(I don't answer questions asked through E-mail or PM's)