Ga naar hoofdinhoud

Hoe Word-documenten batchgewijs naar txt-bestand te converteren?

Dit artikel gaat over het batchgewijs converteren van alle Word-documenten in een specifieke map naar afzonderlijke TXT-bestanden in Word.

Batch converteer Word-documenten naar txt-bestanden met VBA


Batch converteer Word-documenten naar txt-bestanden met VBA

De onderstaande VBA-code kan helpen om alle Word-documenten in een specifieke map in één keer naar txt-bestanden te converteren. Ga als volgt te werk.

1. Druk in Word-document op anders + F11 toetsen om de te openen Microsoft Visual Basic voor toepassingen venster.

2. In de Microsoft Visual Basic voor toepassingen venster klikt Invoegen > Moduleen kopieer vervolgens onderstaande code naar het modulevenster.

VBA-code: Batch converteer Word-documenten naar txt-bestanden

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. druk de F5 sleutel om de code uit te voeren.

4. In de Blader selecteer de map met Word-documenten die u naar txt-bestanden wilt converteren en klik op het OK knop. Zie screenshot:

Vervolgens kunt u zien dat alle documenten in de geselecteerde map in één keer naar txt-bestanden worden geconverteerd. Zie screenshot:

Beste Office-productiviteitstools

Kutools for Word - Verbeter uw woordervaring met Over 100 Opmerkelijke kenmerken!

🤖 Kutools AI-assistent: Transformeer uw schrijven met AI - Inhoud genereren  /  Herschrijf tekst  /  Documenten samenvatten  /  Informeer voor informatie gebaseerd op document, allemaal binnen Word

📘 Documentbeheersing: Pagina's splitsen  /  Voeg documenten samen  /  Selectie exporteren in verschillende formaten (PDF/TXT/DOC/HTML...)  /  Batchconversie naar PDF  /  Pagina's exporteren als afbeeldingen  /  Meerdere bestanden tegelijk afdrukken...

Inhoud bewerken: Batch zoeken en vervangen over meerdere bestanden  /  Formaat van alle afbeeldingen wijzigen  /  Tabelrijen en -kolommen transponeren  /  Converteer tabel naar tekst...

🧹 Moeiteloos schoon: Veeg weg Extra ruimtes  /  Sectie-onderbrekingen  /  Alle koppen  /  Tekstvakken  /  hyperlinks  / Ga voor meer verwijdergereedschappen naar onze Groep verwijderen...

Creatieve invoegingen: Invoegen Duizend scheidingstekens  /  Vink vakjes aan  /  Radio knoppen  /  QR Code  /  Barcode  /  Diagonale lijntabel  /  Vergelijking bijschrift  /  Bijschrift bij afbeelding  /  Tabel titel  /  Meerdere afbeeldingen  / Ontdek meer in de Groep invoegen...

???? Precisieselecties: Vaststellen specifieke pagina's  /  tafels  /  vormen  /  kop paragrafen  / Verbeter de navigatie met meer Selecteer functies...

Sterverbeteringen: Navigeer snel naar elke locatie  /  automatisch herhaalde tekst invoegen  /  naadloos schakelen tussen documentvensters  /  11 Conversiehulpmiddelen...

👉 Wil je deze functies proberen? Kutools voor Word biedt een Gratis proefperiode van 60-dag, zonder beperkingen! 🚀
 
Comments (22)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Vielen Dank, das ist wirklich sehr hilfreich! Ich werde auf Deine Seite bei der nächsten Gelegenheit verweisen.
Gruß
Uli
This comment was minimized by the moderator on the site
Hi, this works perfectly. Is there a way to choose a different encoding format of the TXT (UTF-8 instead of Windows for example) ?
This comment was minimized by the moderator on the site
Hi Simon,
The following VBA code helps to convert all Word documents in a specified folder to UTF-8 .txt files
Sub ConvertDocumentsToTxt()
'Updated by Extendoffice 20201031
    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, ".")
            xDoc.SaveAs Left(xFilePath, xIndex - 1) & ".txt", FileFormat:=wdFormatText, AddToRecentFiles:=False, Encoding:=msoEncodingUTF8
            xDoc.Close True
        End If
        xFileStr = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Merci beaucoup ! La conversion se fait très bien
This comment was minimized by the moderator on the site
Many thanks
This comment was minimized by the moderator on the site
Hello, Thank you for this post. When I hit "run", I get an error message:"Compile Error: Invalid Outside Procedure." This is whether I use the doc or the docx extension (maybe unrelated but I tried both.) Please can you help? I have no idea how to use code.... and really need to convert a ton of docs. Thank you!
This comment was minimized by the moderator on the site
Hi Durga,
The code works well in my case.
Please make sure that the Module (Code) window contains only the VBA code provided in the post.
This comment was minimized by the moderator on the site
That worked a treat! Thanks!
Rated 5 out of 5
This comment was minimized by the moderator on the site
I NEED them to convert to delimited text files is this possible still.
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
The script works great for me, but only for one folder. Is there any way to include all subfolders?
This comment was minimized by the moderator on the site
Thank you a lot! Worked like a charm!
This comment was minimized by the moderator on the site
Worked rapidly and perfectly on a folder full of .docx files. I thank you/
This comment was minimized by the moderator on the site
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?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations