Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何將多個Word文檔批量轉換為pdf文件?

使用Word中的“另存為”功能,很容易將Word文檔轉換為PDF文件。 但是,如果要一次將文件夾中的所有Word文檔轉換為單獨的PDF文件,如何實現? 本文將向您展示VBA代碼,以將多個Word文檔批量轉換為Word中的pdf文件。

使用VBA將多個Word文檔批量轉換為pdf文件
使用Kutools for Word將多個Word文檔批量轉換為pdf文件


使用VBA將多個Word文檔批量轉換為pdf文件

以下VBA代碼可幫助您快速將文件夾中的所有Word文檔立即轉換為pdf文件。 請執行以下操作。

1.在Word中,按 其他 + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口中,單擊 插入 > 模塊,然後將以下VBA代碼複製到“模塊”窗口中。

VBA代碼:在Word中將多個Word文檔批量轉換為pdf文件

Sub ConvertWordsToPdfs()
'Updated by Extendoffice 20181123
    Dim xIndex As String
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim xNewName As String
    Dim xFileName As String
    Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    If xDlg.Show <> -1 Then Exit Sub
    xFolder = xDlg.SelectedItems(1) + "\"
    xFileName = Dir(xFolder & "*.*", vbNormal)
    While xFileName <> ""
        If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then
            xIndex = InStr(xFileName, ".") + 1
            xNewName = Replace(xFileName, Mid(xFileName, xIndex), "pdf")
            Documents.Open FileName:=xFolder & xFileName, _
                ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
                PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
                WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
                wdOpenFormatAuto, XMLTransform:=""
            ActiveDocument.ExportAsFixedFormat OutputFileName:=xFolder & xNewName, _
                ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
                wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
                Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                BitmapMissingFonts:=True, UseISO19005_1:=False
            ActiveDocument.Close
        End If
        xFileName = Dir()
    Wend
End Sub

3。 按 F5 鍵來運行代碼。

4.彈出瀏覽對話框,請選擇包含要轉換為pdf文件的Word文檔的文件夾,然後單擊 OK 按鈕。

然後,所選文件夾中的所有Word文檔將立即轉換為單獨的pdf文件。 看截圖:


使用Kutools for Word將多個Word文檔批量轉換為pdf文件

如果很難處理VBA代碼,則可以嘗試 轉換文件 的效用 Kutools for Word 以解決這個問題。

Kutools for Word :擁有100多個方便的Word加載項, 60天免費試用.

1。 點擊 Kutools Plus > 文件/文件。 看截圖:

2。 在裡面 文件格式轉換器 窗口,您需要執行以下操作:

2.1)在文件夾中選擇包含要轉換為pdf的文檔的文件夾 源文件文件夾 部分;
2.2選擇 將docx轉換成pdf 來自 格式轉換 下拉列表;
2.3)默認情況下, 目標路徑與源相同 框被選中。 如果要將所有已轉換的pdf文件放置在同一文件夾中,只需選中此選項即可;
如果要分開pdf文件和源文檔,則需要取消選中 目標路徑與源相同 框,然後選擇一個新文件夾將pdf文件保存在 儲存 框;
2.4)點擊 開始 按鈕。 看截圖:

3.然後會彈出一個對話框,告訴您已成功轉換了多少文檔,單擊 OK 按鈕並關閉 文件格式轉換器 窗口。

現在,所有.docx格式的文檔都將轉換為pdf文件。

如果您想免費試用該實用程序,請轉到 免費下載軟件 首先,然後按照上述步驟進行操作。



推薦的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.
    Chengchi · 1 months ago
    VBA 很棒!!感謝您的分享。
  • To post as a guest, your comment is unpublished.
    K DEG · 2 months ago
    This saved my day, thanks so much!

    I would just like to point out for people using this, if the Word seems to freeze, just wait - it is generating the .pdf files. My Word froze but I opened the folder of the files, and could see that it was indeed generating the .pdf for all files.
  • To post as a guest, your comment is unpublished.
    Garrie · 2 months ago
    If you change
    xFileName = Dir(xFolder & "*.*", vbNormal)
     to 
    xFileName = Dir(xFolder & "*.doc*", vbNormal)
    the macro will not crash when there are non-Word files in the same folder.

  • To post as a guest, your comment is unpublished.
    Garrie · 2 months ago
    If you change
    xIndex = InStr(xFileName, ".") + 1
     to 
    xIndex = InStrRev(xFileName, ".") + 1
    it will search from the end of the string to find the first period. This will avoid problems where the filename contains periods.
  • To post as a guest, your comment is unpublished.
    Jirka · 3 months ago
    Change
     xIndex = InStr(xFileName, ".") + 1


  • To post as a guest, your comment is unpublished.
    San · 10 months ago
    The VBA code is great. Cheers ,ate
  • To post as a guest, your comment is unpublished.
    Doris · 1 years ago
    thanks for vba code , it helps a lot.
  • To post as a guest, your comment is unpublished.
    Eric Smith · 1 years ago
    Hello, Thank you so much for this code. It is working well, however, I get a dialog box after each conversion to save the word doc and I have to save. Can you include code in the macro to make that save and close the file instead of having to close each one?
    • To post as a guest, your comment is unpublished.
      Rebecca · 10 months ago
      if you add "ActiveDocument.Save" right above "ActiveDocument.Close" it will save the document before closing itself so you don't have to always hit the save prompt.
  • To post as a guest, your comment is unpublished.
    9aks92 · 1 years ago
    Hi,

    I get runtime error '424':
    Object required

    image attached.


    Please help.

    Thanks.
  • To post as a guest, your comment is unpublished.
    Akshay · 1 years ago
    Hi,

    I get runtime error '424':
    Object required

    at the below code

    Documents.Open Filename:=xFolder & xFileName, _
    ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
    WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
    wdOpenFormatAuto, XMLTransform:=""


    wdOpenFormatAuto = 0 it says.

    Please help.

    Thanks.
  • To post as a guest, your comment is unpublished.
    Stefan · 1 years ago
    Hi there,


    is there an easy way to adjust the VBA Code so that the Documents are convertet to .txt-Files?
  • To post as a guest, your comment is unpublished.
    Sammy · 2 years ago
    Is there any way to have the file name taken from the text of the document?
  • To post as a guest, your comment is unpublished.
    Marzio · 2 years ago
    VBA if i try to convert file that contains macros the macro swith to open file and ends without close file.
  • To post as a guest, your comment is unpublished.
    michaelwaung · 2 years ago
    So nice blog, Thanks for sharing this blog. It Has so useful information for users. I liked this information so much.
    Hope you keep sharing such kind of information convert word doc to html