Answer the question
In order to leave comments, you need to log in
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
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
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?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question