How to Find and Replace Contents in Multiple Word Documents

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

  1. First and foremost, put all file in one folder and open Word.
  2. Then click “Developer” tab.
  3. Next click “Visual Basic”. Or simply press “Alt+ F11” to trigger the VBA editor.Click "Developer"->Click "Visual Basic"
  4. Then click “Normal” project.
  5. Next click “Insert” tab and then choose “Module”.Click "Normal"->Click "Insert"->Click "Module"
  6. Now you’ve successfully inserted a new module. Double click on it to open coding space.
  7. 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
  1. Lastly, click “Run”.Paste Codes->Click "Run"
  2. Now there will be an input box popping up. Enter the folder path where you store documents.An input box
  3. Then click “OK” and there is the second box. Enter the texts you want to find.
  4. 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

Comments are closed.