A
A
AlexVrag2012-02-01 09:35:32
Image processing
AlexVrag, 2012-02-01 09:35:32

Batch replace text with another in a folder

There is a My Documents folder in which there are a lot of files.
Task: you need to change line1 to line2 in all files (.doc and .docx).
Tell me what or how?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Belugin, 2012-02-01
@unkinddragon

Using a VBA macro in Word.
I would use a button that calls the file open dialog, and then iteratively call a function for each file that opens it, replaces the string, and closes it.

Public Sub SelectFilesForImport()
    Dim fd As FileDialog
    Dim objfl As Variant
    Dim filnam As String
        
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .AllowMultiSelect = True
        .Filters.Add "Word", "*.doc;*.docx;*.docm"
        .InitialView = msoFileDialogViewDetails
        .Show
        For Each objfl In .SelectedItems
            filnam = objfl
            Call ReplaceInFile(filnam)
        Next objfl
        On Error GoTo 0
    End With
    
    Set fd = Nothing
    
End Sub

Private Sub ReplaceInFile(FileName As String)
    ' Open 
    Set childword = Application.Documents.Open(FileName)
    
    With childword.Content.Find
        .Text = "$%Registrador%$" ' Your search string
        .Replacement.Text = ":O" ' Replace with this
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute replace:=wdReplaceAll
    End With

    childword.Save
    childword.Close
    
End Sub

R
rasa, 2012-02-01
@rasa

I was looking for free software that can do this. Have not found. Weird.
Everywhere is a reference to the already mentioned replacement macro. By the way, here are its options and discussion on the site wordexpert.ru
PS Interesting: does anyone know a free program that can do this?

A
AlexVrag, 2012-02-01
@AlexVrag

Thank you.
Found a working version: wordexpert.ru/page/makros-paketnoj-zameny-ot-aleksandra-vitera

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question