VBA & Macro Problems

Run a Microsoft Word Macro on Multiple TXT Files

Phi Hung VoPhi Hung Vo Nov 22, 2026 868 views

Question details

Run the same Word VBA find-and-replace operations on multiple plain text files.

Run a Microsoft Word Macro on Multiple TXT Files
Product
Microsoft Word
Device & OS
not provided
Scenario
The user wants to select several TXT files and apply a macro to each one.
Observed behavior
A single find-and-replace test worked, but the complete macro with multiple operations did not.
Before you start

Test with copies of a few representative TXT files, including files with special characters, and check their encoding and line breaks after saving. Make sure your Word installation allows trusted VBA macros to run.

Solution 1Recommended

Open and edit selected TXT files with a Word VBA macro

Use a file picker to apply the same replacements to each selected text file.

The macro opens each TXT file as a Word document, edits its content, saves it, and closes it. Automatic text conversion and saving can change encoding or line breaks, so inspect the test results before processing the full set.

1
Open the VBA editor

In Word, open a document and choose Developer > Visual Basic. In the VBA editor, select Insert > Module.

2
Add the batch macro

Paste this code into the module: Sub BatchEditTxtFiles() Dim picker As FileDialog Dim filePath As Variant Dim doc As Document Dim errorMessage As String On Error GoTo Failed Set picker = Application.FileDialog(msoFileDialogFilePicker) With picker .Title = "Select TXT files" .AllowMultiSelect = True .Filters.Clear .Filters.Add "Text files", "*.txt" If .Show <> -1 Then Exit Sub End With For Each filePath In picker.SelectedItems Set doc = Documents.Open(FileName:=CStr(filePath), ConfirmConversions:=False, AddToRecentFiles:=False) With doc.Content.Find .ClearFormatting .Replacement.ClearFormatting .Text = "old text" .Replacement.Text = "new text" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With doc.Save doc.Close SaveChanges:=wdDoNotSaveChanges Set doc = Nothing Next filePath MsgBox "Finished processing the selected files." Exit Sub Failed: errorMessage = Err.Description On Error Resume Next If Not doc Is Nothing Then doc.Close SaveChanges:=wdDoNotSaveChanges MsgBox "Processing stopped: " & errorMessage End Sub

3
Set the replacements

In the module, replace "old text" and "new text" with your actual search and replacement strings. To add another operation, repeat the .Text, .Replacement.Text, and .Execute lines inside the With doc.Content.Find block.

4
Run and check the test

In Word, choose Developer > Macros, select BatchEditTxtFiles, and click Run. Select the test TXT files in the picker, then reopen them to check the replacements, encoding, and line breaks.

5
Troubleshoot the complete macro

If a single replacement works but the full set fails, return to Developer > Visual Basic and add the remaining replacement operations one at a time. Run the macro on test files after each addition to identify the operation causing the problem.

Open and edit selected TXT files with a Word VBA macro
Files are saved in place: The macro saves each successfully processed file before moving to the next. If processing stops, earlier files may already have been changed.
Free Microsoft Office alternative

Edit text documents with WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing documents. It supports common Microsoft Office formats to make moving everyday work easier, but it does not fix a Microsoft Word VBA macro; macro support and compatibility can vary by edition and platform.

Open and edit text documents in WPS WriterWork with Microsoft Word DOC and DOCX filesUse a familiar document-editing interfaceMove everyday document work to a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Can a Word macro edit a TXT file without opening it in Word?

The document-editing approach shown here requires the TXT file to be opened as a Word document first. VBA can also use separate file-access methods, but those do not use Word's document Find object in the same way.

How do I select more than one TXT file?

Run BatchEditTxtFiles from Developer > Macros. In the file picker, select multiple TXT files using your system's standard multi-selection controls, then confirm the selection.

Why does one replacement work but the complete macro fail?

The issue may be in one of the additional replacement operations or its settings. In Developer > Visual Basic, add and test the operations one at a time to isolate it.

Will Word preserve the original text encoding?

Not necessarily. Word's automatic conversion and save settings can affect encoding or line breaks. Test representative files and inspect the saved results before running the macro on the full set.