Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

6 Answers 6

up vote 4 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
1  
@gentlesea does 'Process Files in Batch Folder' setting not help? –  jay Sep 11 '13 at 11:54

Can't it be done in Microsoft Word itself?

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

http://office.microsoft.com/en-us/word/HA012303921033.aspx

share|improve this answer
    
Just saw your comment on tombull89's post. For word 2003, you can follow these instructions: office.microsoft.com/en-us/word/HP051894331033.aspx –  shrmn May 12 '10 at 10:43

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

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$
Loop

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

        document.Select

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

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

        document.Save
        document.Close
    Loop
Loop

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 at 19:23
2  
Notepad++ can open Microsoft Word documents? –  Daniel Beck Jan 2 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 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
            Application.Dialogs(wdDialogEditReplace).Show
            FirstLoop = False

            Response = MsgBox("Do you want to process the rest of the files in this folder", vbYesNo)
            If Response = vbNo Then Exit Sub
        Else
            With Dialogs(wdDialogEditReplace)
                .ReplaceAll = 1
                .Execute
            End With
        End If
        myDoc.Close SaveChanges:=wdSaveChanges
        myFile = Dir$()
    Wend
End Sub
share|improve this answer
3  
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

protected by slhck Jan 2 at 21:19

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.