How can I batch convert txt to docx or doc files ?

Steve Nicol 21 Reputation points

I have 19,000 txt files that I need to convert to either doc or docx. Is there a way to do this in one batch or do I need to do them individually?

Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
903 questions
{count} votes

Accepted answer
  1. Erin Ding-MSFT 4,456 Reputation points

    Hi @Steve Nicol

    Welcome to Q&A forum.

    Sorry for the previous inappropriate response.
    Please note: as PaulEdstein-5060 said, "Word cannot open .txt files that are simply renamed as .docx.
    Although Word can open .txt files that are simply renamed as .doc, that does not mean they conform to the .doc binary format - they remain plain text files."
    Please check if PaulEdstein-5060's reply is helpful to you.

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

2 additional answers

Sort by: Most helpful
  1. Ezreal95 1 Reputation point

    If you use C#, you can batch convert txt to doc or docx by using Spire.Doc for .NET, which is class library for processing Word documents on .NET platform.

    Install Spire.Doc in to your Visual Studio via NuGet, and then you'll be able to do the batch conversion by running the following code snippet.

    using System;  
    using System.IO;  
    using Spire.Doc;  
    namespace CreateWordFromTxt  
        class Program  
            static void Main(string[] args)  
                string inputFolder = @"C:\Users\Administrator\Desktop\TxtFileFolder";  
                string outputFolder = @"C:\Users\Administrator\Desktop\GeneratedWord\";  
                Document doc;  
                string fileNameWithoutExtension;  
                string outputFilePath;  
                DirectoryInfo folder = new DirectoryInfo(inputFolder);  
                foreach (FileInfo file in folder.GetFiles())  
                    doc = new Document();  
                    fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FullName);  
                    outputFilePath = String.Format(outputFolder + fileNameWithoutExtension + ".docx");  
                    doc.SaveToFile(outputFilePath, FileFormat.Docx2013);  


    0 comments No comments

  2. Anonymous

    erinding's 'solution' does not work!

    Word cannot open .txt files that are simply renamed as .docx.

    Although Word can open .txt files that are simply renamed as .doc, that does not mean they conform to the .doc binary format - they remain plain text files.

    To do it properly, you need a macro like:

    Option Explicit
    Dim FSO As Object, oFolder As Object, StrFolds As String
    Sub Main()
    Application.ScreenUpdating = False
    Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
    TopLevelFolder = GetFolder
    StrFolds = vbCr & TopLevelFolder
    If FSO Is Nothing Then
      Set FSO = CreateObject("Scripting.FileSystemObject")
    End If
    'Get the sub-folder structure
    Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
    For Each aFolder In TheFolders
      RecurseWriteFolderName (aFolder)
    'Process the documents in each folder
    For i = 1 To UBound(Split(StrFolds, vbCr))
      Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
    Application.ScreenUpdating = True
    End Sub
    Sub RecurseWriteFolderName(aFolder)
    Dim SubFolders As Variant, SubFolder As Variant
    Set SubFolders = FSO.GetFolder(aFolder).SubFolders
    StrFolds = StrFolds & vbCr & CStr(aFolder)
    On Error Resume Next
    For Each SubFolder In SubFolders
      RecurseWriteFolderName (SubFolder)
    End Sub
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function
    Sub UpdateDocuments(oFolder As String)
    Dim strFldr As String, strFile As String, wdDoc As Document
    strFldr = oFolder: If strFldr = "" Then Exit Sub
    strFile = Dir(strFldr & "\*.txt", vbNormal)
    While strFile <> ""
      Set wdDoc = Documents.Open(FileName:=strFldr & "\" & strFile, AddToRecentFiles:=False, ReadOnly:=False, Visible:=False)
      With wdDoc
        'Save as docx
        .SaveAs2 FileName:=Split(.FullName, ".txt")(0) & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        'close the document
        .Close False
      End With
      'Delete the old file
      'Kill strFldr & "\" & strFile
      strFile = Dir()
    Set wdDoc = Nothing
    End Sub

    The above code processes all .txt files in the selected folder and its sub-folders. Note the commented-out code to delete the .txt file once it has been converted.

    For PC macro installation & usage instructions, see:

    2 people found this answer helpful.
    0 comments No comments