Tutorials > Advanced

Bulk Find and Replace in Word Using VBA

By BulkReplacer Team 6 min read

Visual Basic for Applications (VBA) is the programming language built into Microsoft Office. If you need to manipulate hundreds of files and you are comfortable with code, writing a macro is a powerful solution.

The Logic

The script needs to perform three actions in a loop:

  1. Identify all files in a specific folder.
  2. Open each file one by one in Word (hidden in the background).
  3. Run a Find/Replace command.
  4. Save and close the file.
Advertisement

Sample VBA Code

Here is a simplified snippet to get you started. Press Alt + F11 to open the VBA editor in Word, insert a module, and paste this:

Sub BatchReplace()
    Dim strFolder As String
    Dim strFile As String
    Dim wdDoc As Document
    
    ' Define your folder path here
    strFolder = "C:\Your\Docs\Folder\"
    strFile = Dir(strFolder & "*.docx")

    Do While strFile <> ""
        Set wdDoc = Documents.Open(FileName:=strFolder & strFile, Visible:=False)
        
        With wdDoc.Content.Find
            .Text = "OldText"
            .Replacement.Text = "NewText"
            .Execute Replace:=wdReplaceAll
        End With
        
        wdDoc.Save
        wdDoc.Close
        strFile = Dir
    Loop
End Sub
            

The Dangers of VBA

While powerful, VBA has downsides:

Want a safer, faster way?

Our browser-based tool works without macros, doesn't require installation, and is much faster than VBA.

Use Safe Online Replacer