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 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

3。 按 F5 鍵來運行代碼。

4。 在裡面 瀏覽 窗口中,選擇包含要轉換為txt文件的Word文檔的文件夾,然後單擊 OK 按鈕。 看截圖:

然後,您可以看到所選文件夾中的所有文檔都立即轉換為txt文件。 看截圖:



推薦的Word生產力工具

 

Kutools For Word-超過100種Word高級功能,節省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 · 3 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