吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 768|回复: 41
收起左侧

[其他原创] VBA开发的Word批量转PDF工具

  [复制链接]
xhlbudd 发表于 2026-5-7 22:26
自用的小工具,可以批量把指定文件夹及其子文件夹下所有Word文件(doc或者docx格式均可)转为PDF格式,同时把Word文件中的标题转为PDF中的书签,方便浏览;转换前锁定域,避免出现串行和空白页。希望对大家有用~~

小工具使用VBA开发,轻盈小巧(仅96KB),同时把源代码分享如下,也请吾爱的大神们多多指点:
[Visual Basic] 纯文本查看 复制代码
Private Sub CommandButton1_Click()
    ' 定义变量
    Dim fso As Object ' FileSystemObject
    Dim sourceFolder As String
    Dim targetParentFolder As String
    
    ' 创建文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' 让用户选择源文件夹(包含要转换的doc和docx文件)
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Word批量转PDF by xhlbudd@52pojie"
        If .Show <> -1 Then Exit Sub ' 用户取消了操作
        sourceFolder = .SelectedItems(1)
    End With
    
    ' 在源文件夹的同级目录创建目标总文件夹(如果不存在)
    targetParentFolder = sourceFolder & "_PDF"
    If Not fso.FolderExists(targetParentFolder) Then
        fso.CreateFolder targetParentFolder
    End If
    
    ' 开始递归转换
    ConvertDocsInFolder fso, sourceFolder, targetParentFolder
    
    ' 完成提示
    MsgBox "转换完成,转换后的文件保存在:" & vbCrLf & targetParentFolder
End Sub

Sub ConvertDocsInFolder(fso As Object, currentFolderPath As String, currentTargetParent As String)
    ' 变量定义
    Dim folder As Object
    Dim subFolder As Object
    Dim file As Object
    Dim docFile As Object
    Dim targetFilePath As String
    Dim doc As Document
    
    ' 获取当前文件夹对象
    Set folder = fso.GetFolder(currentFolderPath)
    
    ' 在当前文件夹对应的目标父目录下,创建相同的子文件夹结构
    Dim relativePath As String
    relativePath = Mid(currentFolderPath, Len(fso.GetParentFolderName(currentTargetParent)) + 2)
    Dim targetFolderPath As String
    targetFolderPath = fso.BuildPath(currentTargetParent, relativePath)
    
    If Not fso.FolderExists(targetFolderPath) Then
        fso.CreateFolder targetFolderPath
    End If
    
    ' 遍历当前文件夹中的所有文件
    For Each file In folder.Files
        ' 检查文件扩展名是否为doc或docx(不区分大小写)
        If LCase(fso.GetExtensionName(file.Name)) Like "doc*" Then
            ' 构建目标文件路径(将扩展名改为.docx)
            targetFilePath = fso.BuildPath(targetFolderPath, fso.GetBaseName(file.Name) & ".pdf")
            
            ' 转换文件
            On Error Resume Next ' 错误处理,如文件损坏或正在被使用则跳过
            
            Set doc = Documents.Open(file.Path, False, True, False) ' 以只读方式打开,避免锁定原文件
                       
            '转换为PDF之前锁定所有域
            For Each fieldLoop In doc.Fields
                fieldLoop.Locked = True
            Next fieldLoop
            
            If Not doc Is Nothing Then
                ' 使用ExportAsFixedFormat2方法,并设置书签参数
                doc.ExportAsFixedFormat2 _
                    OutputFileName:=targetFilePath, _
                    ExportFormat:=wdExportFormatPDF, _
                    CreateBookmarks:=wdExportCreateHeadingBookmarks
                doc.Close SaveChanges:=False
            End If

            On Error GoTo 0 ' 恢复错误处理
        End If
    Next file
    
    ' 递归处理所有子文件夹
    For Each subFolder In folder.SubFolders
        ConvertDocsInFolder fso, subFolder.Path, currentTargetParent
    Next subFolder
End Sub

PDF中的书签

PDF中的书签

Word中的标题

Word中的标题

软件界面

软件界面

Word批量转PDF by xhlbudd@52pojie.rar

445.72 KB, 下载次数: 146, 下载积分: 吾爱币 -1 CB

软件及测试文件

免费评分

参与人数 6吾爱币 +4 热心值 +6 收起 理由
luckydat + 1 + 1 谢谢@Thanks!
gzodwn + 1 + 1 谢谢@Thanks!
春天的萌动 + 1 + 1 用心讨论,共获提升!
vipxh + 1 谢谢@Thanks!
小哥仔 + 1 鼓励转贴优秀软件安全工具和文档!
lengrusong + 1 + 1 鼓励转贴优秀软件安全工具和文档!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lizhipei78 发表于 2026-5-8 20:05
这个是我好早写的,可以保存在原文件夹下。
2026-05-08_200423.png
VBA-Word文档批量转PDF.rar (36.71 KB, 下载次数: 2)
lizhipei78 发表于 2026-5-8 19:58
改一句代码,WPS也可用,另存为PDF改为另一种方式
[Asm] 纯文本查看 复制代码
doc.SaveAs2 FileName:=targetFilePath, FileFormat:=17

VBA-Word批量转PDF.rar (24.09 KB, 下载次数: 5)
xiaomingtt 发表于 2026-5-8 09:44
以前做过一个VBS版的。把word文件拖到VBS文件上就自动另存为PDF。
[Visual Basic] 纯文本查看 复制代码
set args = wscript.arguments
if args.count = 0 then wscript.quit
set word = createobject("word.application")
word.visible = false
for i = 0 to args.count - 1
	f = lcase(args(i))
	if right(f,3) = "doc" or right(f,4) = "docx" then
		a = right(f,len(f) - instrrev(f,"\"))
		p = left(f,Instrrev(f,"\")) & left(a,instrrev(a,".")) & "pdf"
		word.documents.open f
		set act = word.ActiveDocument.Activewindow.View
		act.ShowRevisionsAndComments = false
		act.revisionsview = 0
		word.documents(f).activate
		word.activedocument.saveas p,17
		word.activedocument.close 0
	end if
next
word.quit
msgbox "Word转PDF完成",64

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
aranya + 1 + 1 我很赞同!

查看全部评分

WX2886 发表于 2026-5-8 00:16
好工具!先收藏了
zhufuan 发表于 2026-5-8 04:29
老师,这个批量可以转换excel吗
zhufuan 发表于 2026-5-8 05:34
是个空文件,什么都没有
小哥仔 发表于 2026-5-8 06:53
小巧精干 支持excel就更完美了
wang77212 发表于 2026-5-8 07:10
谢谢分享
vipxh 发表于 2026-5-8 07:39
这工具确实优秀,谢谢分享
yobues 发表于 2026-5-8 07:40
学习了,借鉴一下利用ai开发一个EXCEL转pdf的
jsdtzqj 发表于 2026-5-8 07:49
建议啥时候能支持excel就OK了
jxj7823 发表于 2026-5-8 08:00
不知道怎么用
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-5-9 10:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表