This article discusses how to convert all Word documents in a specific folder into separate TXT files using VBA in Word.
Batch Conversion of Word Documents to TXT using VBA
The VBA code below can help convert all Word documents in a specific folder into TXT files simultaneously. Follow these steps:
In the Word document, press Alt + F11 to open the Microsoft Visual Basic for Application window.
In the Microsoft Visual Basic for Application window, click Insert > Module, then copy the code below into the Module window.
VBA Code: Batch Conversion of Word Documents to TXT
Sub ConvertDocumentsToTxt()
‘Updated by Extendoffice 20181123
Dim xIndex As Long
Dim xFolder As Variant
Dim xFileStr As String
Dim xFilePath As String
Dim xDlg As FileDialog
Dim xActPath As String
Dim xDoc As Document
Application.ScreenUpdating = False
Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xDlg.Show <> -1 Then Exit Sub
xFolder = xDlg.SelectedItems(1)
xFileStr = Dir(xFolder & “\*.doc”)
xActPath = ActiveDocument.Path
While xFileStr <> “”
xFilePath = xFolder & “\” & xFileStr
If xFilePath <> xActPath Then
Set xDoc = Documents.Open(xFilePath, AddToRecentFiles:=False, Visible:=False)
xIndex = InStrRev(xFilePath, “.”)
Debug.Print Left(xFilePath, xIndex – 1) & “.txt”
xDoc.SaveAs Left(xFilePath, xIndex – 1) & “.txt”, FileFormat:=wdFormatText, AddToRecentFiles:=False
xDoc.Close True
End If
xFileStr = Dir()
Wend
Application.ScreenUpdating = True
End Sub
Press F5 to run the code.
In the Browse window, select the folder containing the Word documents you want to convert to TXT files and click the OK button. See the screenshot:
Afterward, you can see that all documents in the selected folder will be converted into TXT files simultaneously. See the screenshot for reference.