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

    Question

  • Hi all,
    I have about 400 .txt files and I need to convert all of them into doc or docx format.
    The main problem is that when I  open one of them in Word, it asks me to chose the right encoding to be correctly readable.
    When I try to batch converting these files automatically, I got corrupted files, they are unreadable.
    The only one way that gives me a correct output is to open the .txt file in Word, then save it as docx or doc file.

    BUT I need to do that for 400 file, so how can I doing so automatically?

    I need your help urgently.


    Thanks so much in advance.

    Aya.
    Wednesday, September 5, 2012 9:49 PM

Answers

  • I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    In my testing, all the files were created with a .doc extension. Still, you can ensure that it gets added by changing:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
    to:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".doc"), _


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, September 6, 2012 10:18 AM
    • Marked as answer by ZoghbyAya Thursday, September 6, 2012 10:47 AM
    Thursday, September 6, 2012 10:10 AM
  • I have no idea what the issues with 'Convert Doc 7.2' and 'All Office Converter platinum' might be, but the following should be sufficient to do the conversion. Simply point the macro's browser to the folder containing the files to be converted.

    Sub ConvertFiles()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.txt", vbNormal)
    While strFile <> ""
      Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
        Format:=wdOpenFormatEncodedText, Encoding:=msoEncodingUTF8, _
        AddToRecentFiles:=False, Visible:=False)
      wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
        Fileformat:=wdFormatDocument, AddToRecentFiles:=False
      wdDoc.Close SaveChanges:=False
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    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


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Dear Paul,

    I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    • Marked as answer by ZoghbyAya Thursday, September 6, 2012 10:16 AM
    Thursday, September 6, 2012 10:04 AM
  • Can I also save the files in .docx format rather than just .doc ?

    I have tried to replace the .doc in the code by .docx , but it caused a problem.

    Instead of:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".doc"), _
        Fileformat:=wdFormatDocument, AddToRecentFiles:=False
    use:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".docx"), _
         Fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by ZoghbyAya Friday, September 7, 2012 6:31 PM
    Thursday, September 6, 2012 10:08 PM

All replies

  • What process are you using for the batch conversion and, when prompted for the encoding method, which one do you use?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, September 6, 2012 1:46 AM
  • Thank you for your response,
    As for the used process for the batch conversion:

    I have used two programs, and both produces unreadable format files. The first is :
    'Convert Doc 7.2' program, and the other is 'All Office Converter platinum' program.


    According to the encoding method: it is UTF-8


    Regards,
    Aya.
    Thursday, September 6, 2012 6:28 AM
  • So sorry for that :)
    Thursday, September 6, 2012 6:29 AM
  • I have no idea what the issues with 'Convert Doc 7.2' and 'All Office Converter platinum' might be, but the following should be sufficient to do the conversion. Simply point the macro's browser to the folder containing the files to be converted.

    Sub ConvertFiles()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.txt", vbNormal)
    While strFile <> ""
      Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
        Format:=wdOpenFormatEncodedText, Encoding:=msoEncodingUTF8, _
        AddToRecentFiles:=False, Visible:=False)
      wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
        Fileformat:=wdFormatDocument, AddToRecentFiles:=False
      wdDoc.Close SaveChanges:=False
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    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


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, September 6, 2012 7:16 AM
  • Thanks again, But I did not work with macros before, so please help me how can I use this code and where?

    Regards.

    Thursday, September 6, 2012 7:40 AM
  • Batch processing is a normal computer process. But Word has never been considered a batch environment. It is always a hands on environment first. Macros and any sort of 'batch' concept a distant third.

    If you have access to Word 2003, you can try this:

    Word 2003 has a batch conversion wizard.  Go to File/New.../On my Computer / Other Documents tab/batch conversion wizard & follow the prompts.

    Thursday, September 6, 2012 7:59 AM
  • Thanks again, But I did not work with macros before, so please help me how can I use this code and where?


    For installation and usage instructions, see: http://www.gmayor.com/installing_macro.htm

    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, September 6, 2012 8:13 AM
    Thursday, September 6, 2012 8:11 AM
  • I have no idea what the issues with 'Convert Doc 7.2' and 'All Office Converter platinum' might be, but the following should be sufficient to do the conversion. Simply point the macro's browser to the folder containing the files to be converted.

    Sub ConvertFiles()
    Application.ScreenUpdating = False
    Dim strFolder As String, strFile As String, wdDoc As Document
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.txt", vbNormal)
    While strFile <> ""
      Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
        Format:=wdOpenFormatEncodedText, Encoding:=msoEncodingUTF8, _
        AddToRecentFiles:=False, Visible:=False)
      wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
        Fileformat:=wdFormatDocument, AddToRecentFiles:=False
      wdDoc.Close SaveChanges:=False
      strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    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


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Dear Paul,

    I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    • Marked as answer by ZoghbyAya Thursday, September 6, 2012 10:16 AM
    Thursday, September 6, 2012 10:04 AM
  • I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    In my testing, all the files were created with a .doc extension. Still, you can ensure that it gets added by changing:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
    to:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".doc"), _


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Thursday, September 6, 2012 10:18 AM
    • Marked as answer by ZoghbyAya Thursday, September 6, 2012 10:47 AM
    Thursday, September 6, 2012 10:10 AM
  • I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    In my testing, all the files were created with a .doc extension. Still, you can ensure that it gets added by changing:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
    to:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", "") & ".doc", _


    Cheers
    Paul Edstein
    [MS MVP - Word]

    It is now works perfectly, thanks so much.

    Best Regards,

    Aya.

    • Marked as answer by ZoghbyAya Thursday, September 6, 2012 10:16 AM
    • Unmarked as answer by ZoghbyAya Thursday, September 6, 2012 10:47 AM
    Thursday, September 6, 2012 10:16 AM
  • It is now works perfectly, thanks so much.


    So how come you marked your questions as the answer, instead of my solution?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, September 6, 2012 10:27 AM
  • It is now works perfectly, thanks so much.


    So how come you marked your questions as the answer, instead of my solution?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    No, I just marked your code, and its addition. It is just a miss-marking, so sorry.

    • Edited by ZoghbyAya Thursday, September 6, 2012 10:48 AM
    Thursday, September 6, 2012 10:46 AM
  • Dear Paul ,

    I now need to apply other formulation steps on the same set of documents.

    I need :

        1- removing all empty paragraphs (^p^p) from the text,

        2- customize margins to be smaller, and

        3- Minimize the font of the whole document's text with two or three degrees.

    How can I doing so the same way, using macros ?

    Thanks so much.

    Aya.

    Thursday, September 6, 2012 10:55 AM
  • As that's really a different topic, you should start a new thread.

    When you do so, please specify the margin size for (2) and what you mean by (3).


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, September 6, 2012 11:53 AM
  • Ok dear,

    that's it.

    http://social.technet.microsoft.com/Forums/en/word/thread/db4a8425-45c6-4548-9c91-b7257feb4eca


    Aya Zoghby


    • Edited by ZoghbyAya Thursday, September 6, 2012 5:41 PM
    Thursday, September 6, 2012 2:35 PM
  • I have applied the macro, it results a new file for each .txt file in the folder. However, that new file has not any extension, and can't be opened.

    In my testing, all the files were created with a .doc extension. Still, you can ensure that it gets added by changing:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ""), _
    to:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".doc"), _


    Cheers
    Paul Edstein
    [MS MVP - Word]



    Can I also save the files in .docx format rather than just .doc ?

    I have tried to replace the .doc in the code by .docx , but it caused a problem.

    Aya.


    Aya Zoghby

    Thursday, September 6, 2012 9:11 PM
  • Can I also save the files in .docx format rather than just .doc ?

    I have tried to replace the .doc in the code by .docx , but it caused a problem.

    Instead of:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".doc"), _
        Fileformat:=wdFormatDocument, AddToRecentFiles:=False
    use:
    wdDoc.SaveAs2 FileName:=strFolder & "\" & Replace(strFile, ".txt", ".docx"), _
         Fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by ZoghbyAya Friday, September 7, 2012 6:31 PM
    Thursday, September 6, 2012 10:08 PM
  • Hi all,

    I am now need the opposite case in which I have a set of .docx files and I need to save all of them into .txt format with the UTF8 encoding, how can I modify the previous code for doing so correctly.

    Thanks so much.

    Regards.


    Aya Zoghby

    Saturday, September 29, 2012 8:16 PM
  • As this is a different issue, please start a new thread.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, September 29, 2012 10:20 PM
  • Worked a treat, once I'd changed the method to wdDoc.SaveAs.  The wdDoc.SaveAs2 method did not exist in Word 2007.
    Tuesday, October 22, 2013 3:23 PM
  • Hi there,

    I am trying this, but after selecting the folder and clicking OK, nothing happens. I go to the folder and check and nothing new is there. The same .txt files remain there.

    Do I have to do anything or insert all that code on a different place? I am using THISWORKBOOK option

    Tuesday, November 10, 2015 9:02 PM
  • The macros in this thread are for Word - ThisWorkbook is for Excel, not Word.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, November 10, 2015 9:12 PM