batch Find and Replace for docx files

I need to do a batch Find and Replace using about 12 rules on one .docx file.  (This is a linguistics project.)  Some examples of the rules I am using:

Find underlined not bold ??r and make it underlined bold

Find underlined not bold  [oO][uU]r and make it underlined bold

Find double-underlined not bold * and make it single-underlined not bold

The Find and Replace rules should follow the same format as Word's Find and Replace.

Anyone know of a reliable batch Find and Replace program  that will do this?  Most batch Find and Replace software works on a single Find and Replace Rule applied to multiple files, not to multiple rules applied to a single file.  I need to be able to store the rules.

Answer
Answer

Maybe http://gregmaxey.mvps.org/word_tip_pages/vba_find_and_replace.html would help? If not, you can roll your own with a macro and a table

Create a document with a two column table. Enter the words you want to find in the first column and the formatted versions you wish to replace then with in the second column.  Save the file and put the filename and path where indicated.

Sub ReplaceFromTableList()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim sFname As String
Dim sAsk As String
    sFname = "D:\My Documents\Test\Changes.docx"
    Set oDoc = ActiveDocument
    Set oChanges = Documents.Open(Filename:=sFname, Visible:=False)
    Set oTable = oChanges.Tables(1)
    For i = 1 To oTable.Rows.Count
        Set oRng = oDoc.Range
        Set rFindText = oTable.Cell(i, 1).Range
        rFindText.End = rFindText.End - 1
        Set rReplacement = oTable.Cell(i, 2).Range
        rReplacement.End = rReplacement.End - 1
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(findText:=rFindText, _
                              MatchWholeWord:=True, _
                              MatchWildcards:=False, _
                              Forward:=True, _
                              Wrap:=wdFindStop) = True
                oRng.Select
                sAsk = MsgBox("Replace - " & vbCr & oRng & vbCr + vbCr & _
                              "with - " & vbCr & rReplacement, vbYesNo, _
                              "Replace from Table")

                If sAsk = vbYes Then
                    oRng.FormattedText = rReplacement.FormattedText
                End If
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    Next i
    oChanges.Close wdDoNotSaveChanges
End Sub

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated July 1, 2024 Views 6,530 Applies to: