使用 Word 内置批量宏,将word文档批量转pdf

2025年 10月 15日 18点热度 0人点赞 0条评论
本文教你如何使用Word 内置宏,批量将指定文件夹内的 Word 文档(.doc 或 .docx)转换为 PDF,使用环境为windows。

步骤:

1.  打开 Word

2.  Alt + F11 打开 VBA 编辑器

3. 

4. 

Sub BatchConvertWordToPDF()
    Dim dlg As FileDialog
    Dim doc As Document
    Dim folderPath As String
    Dim fileName As String
    Dim pdfPath As String
    Dim file As Variant
 
    '选择文件夹
    Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
    dlg.Title = "请选择要批量转换的文件夹"
    If dlg.Show <> -1 Then Exit Sub
    folderPath = dlg.SelectedItems(1)
 
    '获取文件夹中的所有 Word 文件
    fileName = Dir(folderPath & "\*.doc*")
 
    '循环处理
    Do While fileName <> ""
        Set doc = Documents.Open(folderPath & "\" & fileName, ReadOnly:=True)
        pdfPath = folderPath & "\" & Left(fileName, InStrRev(fileName, ".") - 1) & ".pdf"
        doc.ExportAsFixedFormat OutputFileName:=pdfPath, ExportFormat:=wdExportFormatPDF
        doc.Close SaveChanges:=False
        fileName = Dir
    Loop
 
    MsgBox "转换完成!", vbInformation
End Sub
 

 

5.F5 运行;

6.选择含有 .doc/.docx 文件的文件夹,等待完成。

优点:

 无需第三方软件;

 保留排版、图片、页眉页脚;

 支持 Word 2007+。

admin

这个人很懒,什么都没留下