How to batch convert Word documents to txt file?
This article is talking about how to batch convert all Word documents in a specific folder to separate TXT files in Word.
The below VBA code can help to convert all Word documents in a specific folder to txt files at once. Please do as follows.
1. In Word document, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy below code into the Module window.
VBA code: Batch convert Word documents to txt files
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
3. Press the F5 key to run the code.
4. In the Browse window, select the folder contains Word documents you will convert to txt files, and click the OK button. See screenshot:
Then you can see all documents in selected folder are converted to txt files at once. See screenshot:
Recommended Word Productivity Tools
- Complicated and repeated operations can be done one-time processing in seconds.
- Insert multiple images across folders into Word document at once.
- Merge and combine multiple Word files across folders into one with your desired order.
- Split the current document into separate documents according to heading, section break or other criteria.
- Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
You are guest
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 7 days agoHi, the code works but at the end gives me 'Runtime error 91', some of my files have objects. Any idea how can I fix this?
To post as a guest, your comment is unpublished.· 1 months ago@Mario Would it be possible to provide the same code when I use the latest Word on a Mac? I get the Run-Time error 5948 and debug points to this section: Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)••••ˇˇˇˇ
To post as a guest, your comment is unpublished.· 4 months ago@Mina Change xFileStr = Dir(xFolder & "\*.doc") to xFileStr = Dir(xFolder & "\*.docx") as i assume you have newer word files format.
To post as a guest, your comment is unpublished.· 5 months ago@Mina I'm using windows 10 and Microsoft office 2016
To post as a guest, your comment is unpublished.· 5 months agoNot working for me, any ideas why?
Do I need to do this in a word document from the same folder I want to convert or any new word document that I start? -Tried both options and none is working but I am also not getting any error messages
To post as a guest, your comment is unpublished.· 5 months ago@DebSutter How did you get it to work?
To post as a guest, your comment is unpublished.· 5 months agoThank you! This is extremely helpful
To post as a guest, your comment is unpublished.· 1 years agoI'm a fan of poetry. I needed to convert more than 700 word compositions to txt. Thanks to your subroutine I managed to do it in no time.