Note: The other languages of the website are Google-translated. Back to English
Se connecter  \/ 
x
or
x
S'inscrire  \/ 
x

or

Comment convertir par lots des documents Word en fichier txt?

Cet article explique comment convertir par lots tous les documents Word dans un dossier spécifique pour séparer les fichiers TXT dans Word.

Conversion par lots de documents Word en fichiers txt avec VBA


Conversion par lots de documents Word en fichiers txt avec VBA

Le code VBA ci-dessous peut aider à convertir tous les documents Word d'un dossier spécifique en fichiers txt à la fois. Veuillez faire comme suit.

1. Dans un document Word, appuyez sur la touche autre + F11 clés pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

2. dans le Microsoft Visual Basic pour applications fenêtre, cliquez sur insérer > Module, puis copiez le code ci-dessous dans la fenêtre Module.

Code VBA: convertir par lots des documents Word en fichiers txt

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. appuie sur le F5 clé pour exécuter le code.

4. dans le DECOUVREZ , sélectionnez le dossier contenant les documents Word que vous allez convertir en fichiers txt, puis cliquez sur le OK bouton. Voir la capture d'écran:

Ensuite, vous pouvez voir que tous les documents du dossier sélectionné sont convertis en fichiers txt à la fois. Voir la capture d'écran:


Outils de productivité Word recommandés

 

Kutools For Word - Plus de 100 fonctionnalités avancées pour Word, économisez 50% de votre temps

  • Les opérations compliquées et répétées peuvent être effectuées une seule fois en quelques secondes.
  • Insérez simultanément plusieurs images dans des dossiers dans un document Word.
  • Fusionner et combiner plusieurs fichiers Word dans des dossiers en un seul avec l'ordre souhaité.
  • Divisez le document actuel en documents séparés selon l'en-tête, le saut de section ou d'autres critères.
  • Convertissez des fichiers entre Doc et Docx, Docx et PDF, collection d'outils pour les conversions et la sélection courantes, etc.
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Darren · 2 months ago
    I NEED them to convert to delimited text files is this possible still.

  • To post as a guest, your comment is unpublished.
    Eunjin · 6 months ago
    I used the script above to batch convert documents in Korean, but the script did not work. 
    When I tired to change the encoding, 
    Can anyone help me with this error?
  • To post as a guest, your comment is unpublished.
    Peter · 7 months ago
    Thanks for this - I was just going to code something myself when I thought, "Hey perhaps someone has done this already?" You had, and a deal more elegantly than I would have done. Thanks you for your hard work. 
  • To post as a guest, your comment is unpublished.
    Mark Müller · 7 months ago
    The script works great for me, but only for one folder. Is there any way to include all subfolders?  
  • To post as a guest, your comment is unpublished.
    Vlad · 8 months ago
    Thank you a lot! Worked like a charm!
    • To post as a guest, your comment is unpublished.
      Galen Currah · 8 months ago
      Worked rapidly and perfectly on a folder full of .docx files. I thank you/
  • To post as a guest, your comment is unpublished.
    Clemen · 9 months ago
    Hi, 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.
    Mina · 1 years ago
    Not 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.
      mIk · 1 years ago
      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.
      Mina · 1 years ago
      I'm using windows 10 and Microsoft office 2016
  • To post as a guest, your comment is unpublished.
    DebSutter · 1 years ago
    Thank you! This is extremely helpful
    • To post as a guest, your comment is unpublished.
      Mina · 1 years ago
      How did you get it to work?
  • To post as a guest, your comment is unpublished.
    Mario · 2 years ago
    I'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.
    Thank you
    • To post as a guest, your comment is unpublished.
      CM · 10 months ago
      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)••••ˇˇˇˇ