Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to do search and replace in many Word (.doc) files (for the purpose of automation of the process).

The software I found so far only allows me to search, but not replace.

How can I do that with Word?

share|improve this question
up vote 6 down vote accepted

You could try VBA Find & Replace.

VBA Find and Replace© provides a method for finding and replacing text anywhere in a document (or collection of documents) using single user defined "find" and "replace" variable pairs, or a user defined list of "find" and "replace" pairs. It also provides a method to find text and replace found text with a user defined "AutoText" or "Building Block" entry.

share|improve this answer
This looks just like what I wanted - You rock! – Tal Galili May 13 '10 at 11:52
Which files does this plugin search over? I cannot find a possibility to specify a starting directory. Does it use all open files only? And how do I just search without replacing? – gentlesea Sep 11 '13 at 7:55
@gentlesea does 'Process Files in Batch Folder' setting not help? – Jay Sep 11 '13 at 11:54
There's an extra period on the end of that URL – I'd remove it but StackOverflow is designed incorrectly and prevents that: – Chris Adams Mar 10 '15 at 15:47
@ChrisAdams: thanks buddy, it's fixed now. It didn't let you suggest an edit? – Jay Mar 10 '15 at 23:14

Can't it be done in Microsoft Word itself?

Its in your Edit menu (Word 2003) or Home tab/Editing section (Word 2007)

share|improve this answer
Just saw your comment on tombull89's post. For word 2003, you can follow these instructions: – shrmn May 12 '10 at 10:43
both links are dead now – Boris Callens Feb 5 '15 at 15:44

If you don't own Microsoft Word and are looking for a free alternative which will help you, try

I don't think this will help you if you're looking for a way to automate this process.

share|improve this answer

For a purpose of executing multiple normal and wildcard-based replacements in multiple MS Word files located in multiple folders under given root folder, I have created following VBA macro. In order to use it, you have change content of following variables (constants):

  • rootPath: root folder under which are located folders containing Word documents.
  • findTextsWild and replaceTextsWild: array of wildcard-based find and replace expressions.
  • findTexts and replaceTexts: array of normal find and replace expressions.

May be you will find it usefull :-)

Sub GlobalTextReplacement()
' Root under which all manuals are stored
Dim rootPath As String
rootPath = "c:\Data\Manuals\"

' Find and replace text for wildcard replacement. Performed first.
Dim findTextsWild() As Variant, replaceTextsWild() As Variant
findTextsWild = Array("[ ]{2;}", "[cC]onfiguration[/ ]@[pP]olicy [rR]epository", "[sS]ervlet[- ]@[fF]ilter")
replaceTextsWild = Array(" ", "Configuration/Policy Repository", "Servlet-Filter")

' Find and replace text for normal case insensitive replacement. Performed second.
Dim findTexts() As Variant, replaceTexts() As Variant
findTexts = Array("DirX Access", "Policy Repository", "User Repository", "Servlet", "servletfilter", "SAML assertion", "DirX Access Server", "DirX Access Manager", "Deployment Manager", "Policy Manager", "Client SDK", "^p ", " ^p")
replaceTexts = Array("DirX Access", "Policy Repository", "User Repository", "Servlet", "Servlet-Filter", "SAML assertion", "DirX Access Server", "DirX Access Manager", "Deployment Manager", "Policy Manager", "Client SDK", "^p", "^p")

' Main code
Application.ScreenUpdating = False

Dim dirNames(20) As String
Dim dirNamesCount As Integer
dirNamesCount = 0

Dim dirName As String
dirName = Dir$(rootPath & "*", vbDirectory)
Do Until LenB(dirName) = 0
    Dim dirPath As String
    dirPath = rootPath & dirName
    If ((GetAttr(dirPath) And vbDirectory) = vbDirectory) And (dirName <> ".") And (dirName <> "..") Then
        dirNamesCount = dirNamesCount + 1
        dirNames(dirNamesCount) = dirPath & "\"
    End If
    dirName = Dir$

Do While dirNamesCount > 0
    Dim fileName As String
    dirName = dirNames(dirNamesCount)
    dirNamesCount = dirNamesCount - 1
    fileName = Dir$(dirName & "*.doc", vbDirectory)
    Do Until LenB(fileName) = 0
        Dim filePath As String
        filePath = dirName & fileName
        fileName = Dir$

        Dim document As document
        Set document = Documents.Open(filePath)
        document.TrackRevisions = True


        Dim i As Integer, maxIndex As Integer
        maxIndex = UBound(findTextsWild)
        For i = LBound(findTextsWild) To maxIndex
            With Selection.Find
                .Text = findTextsWild(i)
                .Replacement.Text = replaceTextsWild(i)
                .Execute Replace:=wdReplaceAll, Forward:=True, _
                    Wrap:=wdFindContinue, MatchWildcards:=True
            End With

        maxIndex = UBound(findTexts)
        For i = LBound(findTexts) To maxIndex
            With Selection.Find
                .Text = findTexts(i)
                .Replacement.Text = replaceTexts(i)
                .Execute Replace:=wdReplaceAll, Forward:=True, _
                    Wrap:=wdFindContinue, MatchCase:=False, MatchWildcards:=False
            End With


Application.ScreenUpdating = True
End Sub
share|improve this answer

Notepad++ Is able to do this using "Find in Files"

share|improve this answer
Keyboard shortcut Ctrl+Shift+F – Yitzchak Jan 2 '14 at 19:23
Notepad++ can open Microsoft Word documents? – Daniel Beck Jan 2 '14 at 20:55
This MAY work iff the string match and replacement string are not special characters because Word 2007+ documents are actually XML files by nature. But I said it may. Have you cared to test this before posting as an answer. – tumchaaditya Jan 2 '14 at 21:32
Option Explicit

Public Sub BatchReplaceAll()
    Dim FirstLoop As Boolean
    Dim myFile As String
    Dim PathToUse As String
    Dim myDoc As Document
    Dim Response As Long

    PathToUse = ActiveDocument.Path
    On Error Resume Next
    Documents.Close SaveChanges:=wdPromptToSaveChanges
    FirstLoop = True
    myFile = Dir$(PathToUse & "\*.doc*")

    While myFile <> ""
        Set myDoc = Documents.Open(PathToUse & "\" & myFile)
        If FirstLoop Then
            FirstLoop = False

            Response = MsgBox("Do you want to process the rest of the files in this folder", vbYesNo)
            If Response = vbNo Then Exit Sub
            With Dialogs(wdDialogEditReplace)
                .ReplaceAll = 1
            End With
        End If
        myDoc.Close SaveChanges:=wdSaveChanges
        myFile = Dir$()
End Sub
share|improve this answer
What is this block of code, what does it do, and how can it help solve this problem? – Moses Oct 9 '13 at 15:25

You must log in to answer this question.

protected by slhck Jan 2 '14 at 21:19

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .