Preskoči na glavno vsebino

Kako paketno pretvoriti Wordove dokumente v datoteko txt?

Ta članek govori o tem, kako paketno pretvoriti vse Wordove dokumente v določeni mapi v ločene datoteke TXT v Wordu.

Paketno pretvorite Wordove dokumente v datoteke txt z VBA


Paketno pretvorite Wordove dokumente v datoteke txt z VBA

Spodnja koda VBA vam lahko pomaga pretvoriti vse Wordove dokumente v določeni mapi v datoteke txt hkrati. Naredite naslednje.

1. V Wordovem dokumentu pritisnite druga + F11 tipke za odpiranje Microsoft Visual Basic za aplikacije okno.

2. V Ljubljani Microsoft Visual Basic za aplikacije okno, kliknite Vstavi > Moduli, nato kopirajte spodnjo kodo v okno modula.

Koda VBA: paketno pretvorite Wordove dokumente v datoteke 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. Pritisnite F5 tipko za zagon kode.

4. V Ljubljani Brskanje izberite mapo, v kateri so Wordovi dokumenti, ki jih želite pretvoriti v datoteke txt, in kliknite OK . Oglejte si posnetek zaslona:

Nato lahko vidite, da se vsi dokumenti v izbrani mapi hkrati pretvorijo v datoteke txt. Oglejte si posnetek zaslona:

Najboljša pisarniška orodja za produktivnost

Kutools za Word - Izboljšajte svojo besedno izkušnjo z Over 100 Izjemne lastnosti!

🤖 Kutools AI pomočnik: Preoblikujte svoje pisanje z AI - Ustvarite vsebino  /  Prepiši besedilo  /  Povzemite dokumente  /  Povprašajte za informacije na podlagi dokumenta, vse v Wordu

📘 Obvladovanje dokumentov: Razdeljene strani  /  Spoji dokumente  /  Izvozi izbor v različnih formatih (PDF/TXT/DOC/HTML ...)  /  Paketna pretvorba v PDF  /  Izvozi strani kot slike  /  Natisnite več datotek hkrati...

Urejanje vsebine: Serijsko iskanje in zamenjava čez več datotek  /  Spremeni velikost vseh slik  /  Prenos vrstic in stolpcev tabele  /  Pretvori tabelo v besedilo...

🧹 Čiščenje brez napora: Pometite stran Dodatni prostori  /  Prelomi oddelkov  /  Vse glave  /  Besedilna polja  /  Hiperpovezave  / Za več orodij za odstranjevanje pojdite na naš Odstrani skupino...

Ustvarjalni vložki: Vstavi Tisoč ločila  /  Potrditvena polja  /  Radijske tipke  /  QR koda  /  Črtna koda  /  Tabela z diagonalno črto  /  Naslov enačbe  /  Opis slike  /  Naslov tabele  /  Več slik  / Odkrijte več v Vstavi skupino...

???? Natančne izbire: Natančna točka določene strani  /  mize  /  Oblike  /  odstavki naslova  / Izboljšajte navigacijo z več Izberite funkcije...

Izboljšave zvezdic: Hitra navigacija do katere koli lokacije  /  samodejno vstavljanje ponavljajočega se besedila  /  brez težav preklapljate med okni dokumentov  /  11 Orodja za pretvorbo...

???? Želite preizkusiti te funkcije? Kutools for Word ponuja a 60-dan brezplačni preskus, brez omejitev! 🚀
 
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