question

SteveNicol-2539 avatar image
0 Votes"
SteveNicol-2539 asked Ezreal95-7594 edited

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

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?

office-word-itpro
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@SteveNicol-2539
Tag "office-exchange-online-itpro" focuses on general issues about the configuration and administration of Exchange Online, as your issue is out of the scope of the tag, I will remove it. Thanks for your understanding.

0 Votes 0 ·
erinding-msft avatar image
0 Votes"
erinding-msft answered erinding-msft edited

Hi @SteveNicol-2539

Welcome to Q&A forum.

—————————————Update————————————
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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This will not work! See Paul Edstein's response.

0 Votes 0 ·
$$ANON_USER$$ avatar image
2 Votes"
$$ANON_USER$$ answered $$ANON_USER$$ edited

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)
 Next
 'Process the documents in each folder
 For i = 1 To UBound(Split(StrFolds, vbCr))
   Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
 Next
 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)
 Next
 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()
 Wend
 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: http://www.gmayor.com/installing_macro.htm



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ezreal95-7594 avatar image
0 Votes"
Ezreal95-7594 answered Ezreal95-7594 edited

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();
                 doc.LoadText(file.FullName);
                 fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FullName);
                 outputFilePath = String.Format(outputFolder + fileNameWithoutExtension + ".docx");
                 doc.SaveToFile(outputFilePath, FileFormat.Docx2013);
             }
    
         }
     }
 }

126580-2021-08-26-160022.png



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.