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:
- Identify all files in a specific folder.
- Open each file one by one in Word (hidden in the background).
- Run a Find/Replace command.
- 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:
- Security Risks: Macros can contain viruses, so IT departments often block them.
- No Undo: Once the macro runs and saves, you can't hit Ctrl+Z to undo changes in 100 files.
- Slow: It physically opens Word for every file, which consumes memory and time.
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