question

SteveNicol-2539 avatar image
0 Votes"
SteveNicol-2539 asked ·

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
10 |1000 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 ·

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 ·
10 |1000 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 ·
PaulEdstein-5060 avatar image
2 Votes"
PaulEdstein-5060 answered ·

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



·
10 |1000 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.