跳至主要内容

如何批量将Word文档转换为txt文件?

Author: Siluvia Last Modified: 2025-05-07

本文讨论如何将特定文件夹中的所有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 功能AI助手 / 实时助手 / 超级润色(保留格式)/ 超级翻译(保留格式)/ AI遮挡 / AI校正...

📘 文档精通拆分页面 / 合并文档 / 以多种格式导出选择内容(PDF/TXT/DOC/HTML...)/ 批量转换为 PDF...

内容编辑跨多个文件批量查找和替换 / 调整所有图片大小 / 翻转表格的行和列 / 表格转文本...

🧹 轻松清理:清除多余空格 / 分节符 / 文本框 / 超链接 / 更多清理工具,请前往“清除”组...

创意插入:插入千位分隔符 / 复选框 / 单选按钮 / 二维码 / 条形码 / 多张图片 / 在“插入 ”组中发现更多...

🔍 精确选择:定位特定页面 / 表格 / 形状 / 标题段落 / 使用更多 选择 功能增强导航...

星级增强功能跳转到任意位置 / 自动插入重复文本 / 在文档窗口之间切换 / 11 转换 工具...

Kutools and Kutools Plus tabs on the Word Ribbon
👉 想尝试这些功能吗?立即下载 Kutools for Word!🚀
 

最佳办公生产力工具

Kutools for Word - 100+ Word 工具