Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

如何批次將Word文檔轉換為txt文件?

Author: Siluvia Last Modified: 2025-08-23

本文將介紹如何在Word中將特定文件夾中的所有Word文檔批次轉換為單獨的TXT文件。

使用VBA將Word文檔批次轉換為txt文件


使用VBA將Word文檔批次轉換為txt文件

以下VBA代碼可以幫助您一次性將特定文件夾中的所有Word文檔轉換為txt文件。請按照以下步驟操作。

1. 在Word文檔中,按下 Alt + F11 鍵以打開Microsoft Visual Basic for Applications窗口。

2. 在Microsoft Visual Basic for Applications窗口中,點擊插入 > 模組,然後將以下代碼複製到模組窗口中。

VBA代碼:批次將Word文檔轉換為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

VBA window with the above code pasted into a module

3. 按下 F5 鍵來運行代碼。

4. 在瀏覽窗口中,選擇包含要轉換為txt文件的Word文檔的文件夾,然後點擊確定按鈕。參見截圖:

Browse window

然後您可以看到所選文件夾中的所有文檔都一次性轉換為txt文件。參見截圖:

All documents in selected folder are converted to txt files

最佳辦公效率工具

Kutools for Word -透過超過 100 項卓越功能,提升您的 Word 使用體驗!

🤖 Kutools AI FeaturesAI AssistantReal-Time AssistantSuper Polish(保留格式)Super Translate(保留格式)AI RedactionAI Proofread...

📘 文件掌握分割頁面文檔合併區域導出成各種格式(PDF/TXT/DOC/HTML...)批量轉換為 PDF...

正文編輯批量查找與替換多個文件批量調整所有圖片大小翻轉表格的行與列表格轉文本...

🧹 輕鬆清理:快速清除多餘空格分節符文字框超連結/更多清除工具請前往刪除工具組...

創意插入:插入千位分隔符複選框選項按鈕二維碼條形碼批量插入圖片/還有更多精彩功能在插入工具組...

🔍 精確選擇:精確定位特定頁面表格形狀標題段落/使用更多 選擇 功能提升導航效率...

亮點增強導航至任何區域自動插入重複文字切換文檔窗口11 轉換工具...

🌍 支援40+種語言:可用 Kutools於您偏好語言環境——支援英語、西班牙語、德語、法語、中文以及超過40種語言!

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想立即體驗這些功能嗎?立即下載 Kutools for Word!🚀
 

✨ Kutools for Office——一次安裝,五大強力工具!

包含 Office Tab Pro · Kutools for Excel · Kutools for Outlook · Kutools for Word · Kutools for PowerPoint

📦 五大工具一次全擁有 | 🔗 與 Microsoft Office 無縫整合 | ⚡ 即刻省時提高工作效率

最佳辦公效率工具

Kutools for Word - 100+ 款 Word 工具