Bỏ qua nội dung chính

Làm thế nào để chuyển đổi hàng loạt tài liệu Word sang tệp txt?

Bài viết này nói về cách chuyển đổi hàng loạt tất cả các tài liệu Word trong một thư mục cụ thể để tách các tệp TXT trong Word.

Chuyển đổi hàng loạt tài liệu Word sang tệp txt với VBA


Chuyển đổi hàng loạt tài liệu Word sang tệp txt với VBA

Mã VBA dưới đây có thể giúp chuyển đổi tất cả các tài liệu Word trong một thư mục cụ thể thành tệp txt cùng một lúc. Hãy làm như sau.

1. Trong tài liệu Word, nhấn Khác + F11 phím để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.

2. bên trong Microsoft Visual Basic cho các ứng dụng cửa sổ, nhấp Chèn > Mô-đun, sau đó sao chép mã bên dưới vào cửa sổ Mô-đun.

Mã VBA: Chuyển đổi hàng loạt tài liệu Word sang tệp 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. Nhấn nút F5 phím để chạy mã.

4. bên trong Xem cửa sổ, chọn thư mục chứa tài liệu Word mà bạn sẽ chuyển đổi sang tệp txt và nhấp vào OK cái nút. Xem ảnh chụp màn hình:

Sau đó, bạn có thể thấy tất cả các tài liệu trong thư mục đã chọn được chuyển đổi thành tệp txt cùng một lúc. Xem ảnh chụp màn hình:

Công cụ năng suất văn phòng tốt nhất

Kutools cho Word - Nâng cao trải nghiệm sử dụng từ của bạn với Over 100 Các tính năng đáng chú ý!

🤖 Trợ lý AI của Kutools: Biến đổi bài viết của bạn bằng AI - Tạo nội dung  /  Viết lại văn bản  /  Tóm tắt tài liệu  /  Hỏi thông tin dựa trên tài liệu, tất cả đều có trong Word

📘 Làm chủ tài liệu: Chia trang  /  Hợp nhất tài liệu  /  Lựa chọn xuất khẩu ở nhiều định dạng khác nhau (PDF/TXT/DOC/HTML...)  /  Chuyển đổi hàng loạt sang PDF  /  Xuất trang dưới dạng hình ảnh  /  In nhiều tập tin cùng một lúchữu ích. Cảm ơn !

Chỉnh sửa nội dung: Tìm và thay thế hàng loạt trên nhiều tệp  /  Thay đổi kích thước tất cả hình ảnh  /  Hoán đổi hàng và cột của bảng  /  Chuyển bảng thành văn bảnhữu ích. Cảm ơn !

🧹 Dễ dàng làm sạch: Quét đi Không gian bổ sung  /  Phần ngắt  /  Tất cả các tiêu đề  /  Hộp văn bản  /  Các siêu liên kết  / Để biết thêm các công cụ xóa, hãy truy cập Xóa nhómhữu ích. Cảm ơn !

Phụ trang quảng cáo: Chèn Ngàn dấu phân cách  /  Hộp kiểm tra  /  Nút radio  /  Mã QR  /  Mã vạch  /  Bảng đường chéo  /  Chú thích phương trình  /  Hình ảnh Caption  /  Bảng chú thích  /  Nhiều Hình Ảnh  / Khám phá thêm trong Chèn nhómhữu ích. Cảm ơn !

???? Lựa chọn chính xác: Xác định trang cụ thể  /  bảng  /  hình dạng  /  đoạn văn tiêu đề  / Tăng cường điều hướng với chi tiết Chọn tính nănghữu ích. Cảm ơn !

Cải tiến sao: Điều hướng nhanh chóng đến bất kỳ vị trí nào  /  tự động chèn văn bản lặp đi lặp lại  /  chuyển đổi liền mạch giữa các cửa sổ tài liệu  /  11 công cụ chuyển đổihữu ích. Cảm ơn !

👉 Bạn muốn thử những tính năng này? Kutools cho Word cung cấp một 60 ngày dùng thử miễn phí, không có giới hạn! 🚀
 
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