In today’s article, we are thrilled to inform you of the way to find and replace contents in multiple Word documents.
Generally speaking, most Word users are quite familiar with the “Find and Replace” feature in the software. It helps us with the batch process through the entire document, which saves us a lot of time. Then have you thought about using this option in multi-document, like all files under a specific folder?
Since there is always the need to search and replace contents in a batch of files, it would be nice to have the function realized. And our way is to use the macro. Please read on to find out more details.
Steps to Install and Run Macro
- First and foremost, put all file in one folder and open Word.
- Then click “Developer” tab.
- Next click “Visual Basic”. Or simply press “Alt+ F11” to trigger the VBA editor.
- Then click “Normal” project.
- Next click “Insert” tab and then choose “Module”.
- Now you’ve successfully inserted a new module. Double click on it to open coding space.
- Paste the bellowing codes there:
Sub FindAndReplaceInFolder() Dim objDoc As Document Dim strFile As String Dim strFolder As String Dim strFindText As String Dim strReplaceText As String ' Pop up input boxes for user to enter folder path, the finding and replacing texts. strFolder = InputBox("Enter folder path here:") strFile = Dir(strFolder & "\" & "*.docx", vbNormal) strFindText = InputBox("Enter finding text here:") strReplaceText = InputBox("Enter replacing text here:") ' Open each file in the folder to search and replace texts. Save and close the file after the action. While strFile <> "" Set objDoc = Documents.Open(FileName:=strFolder & "\" & strFile) With objDoc With Selection .HomeKey Unit:=wdStory With Selection.Find .text = strFindText .Replacement.text = strReplaceText .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End With objDoc.Save objDoc.Close strFile = Dir() End With Wend End Sub
- Lastly, click “Run”.
- Now there will be an input box popping up. Enter the folder path where you store documents.
- Then click “OK” and there is the second box. Enter the texts you want to find.
- Continue to click “OK” to open the third input box, in which you enter the replacing texts and click the “OK” button there. The macro will automatically replace all the contents and save the change.
As you see, this macro provides you with 3 input boxes where you can enter folder path, the find and replacing texts. So you can assign a button for this macro and add it to the “Quick Access Toolbar” for reuse.
As for how to assign a button to a macro, you can read this article: How to Remove the Formatting of Pasted Texts with Macro and VBA in Your Word
Protect Valuable Data
Errors and problems can occur in Word. As a consequence, precious documents can get damaged. While encountering with doc damage, it’s advisable to get hold of a professional recovery tool so as to retrieve the data.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including xls damage and pdf repair software products. For more information visit www.datanumen.com
PLEASE POST A MACRO FOR REPLACING WORDS IN HEADERS, SO it would be easy to change many documents
PLEASE POST A MACRO FOR REPLACING WORDS IN HEADERS
yhgroiodlxzzdtuujvrauvxvzeochs
unfortunately, it can not be used to replace Vietnamese because of font errors
This ROCKS! You don’t know how much time this saved me. Thank you very much for publishing this article.
i keep trying and getting “code execution has been interrupted” I am not sure what I am missing! Help please 🙂
This is great. So glad it was posted and I was able to find it. Thank you, thank you.
Simple. It works well. Thank you!
I’ll PayPal you $10 if you add into this script how to change words in COMMENTS fields and headers and footers as well!
you saved me a lot of time! THANKS A LOT.
Thank you so much for this post, it was almost a life saver. However the documents I need to find and replace text in are all Word Template files. Is there any chance you have some advice on updating this to replace the text in templates?