Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
点此注册  \/ 
x

or

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

本文讨论的是如何将特定文件夹中的所有Word文档批量转换为Word中的单独TXT文件。

使用VBA将Word文档批量转换为txt文件


使用VBA将Word文档批量转换为txt文件

下面的VBA代码可以帮助将特定文件夹中的所有Word文档立即转换为txt文件。 请执行以下操作。

1.在Word文档中,按 其他 + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 在里面 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 模块,然后将以下代码复制到“模块”窗口中。

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

3。 按 F5 键来运行代码。

4。 在里面 浏览 窗口中,选择包含要转换为txt文件的Word文档的文件夹,然后单击 OK 按钮。 看截图:

然后,您可以看到所选文件夹中的所有文档都立即转换为txt文件。 看截图:



推荐的Word生产力工具

 

Kutools For Word-Word超过100种高级功能,可节省50%的时间

  • 复杂和重复的操作可以在几秒钟内完成一次处理。
  • 跨文件夹一次将多个图像插入Word文档。
  • 将文件夹中的多个Word文件合并并合并为所需顺序。
  • 根据标题,分节符或其他条件将当前文档拆分为单独的文档。
  • 在Doc和Docx之间,Docx和PDF之间转换文件,用于常见转换和选择的工具集合等...

Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Mina · 3 months ago
    Not working for me, any ideas why?
    Do I need to do this in a word document from the same folder I want to convert or any new word document that I start? -Tried both options and none is working but I am also not getting any error messages

    • To post as a guest, your comment is unpublished.
      mIk · 3 months ago
      Change xFileStr = Dir(xFolder & "\*.doc") to xFileStr = Dir(xFolder & "\*.docx") as i assume you have newer word files format.
    • To post as a guest, your comment is unpublished.
      Mina · 3 months ago
      I'm using windows 10 and Microsoft office 2016
  • To post as a guest, your comment is unpublished.
    DebSutter · 4 months ago
    Thank you! This is extremely helpful
    • To post as a guest, your comment is unpublished.
      Mina · 3 months ago
      How did you get it to work?
  • To post as a guest, your comment is unpublished.
    Mario · 1 years ago
    I'm a fan of poetry. I needed to convert more than 700 word compositions to txt. Thanks to your subroutine I managed to do it in no time.
    Thank you