|
|

楼主 |
发表于 2025-1-5 14:03
|
显示全部楼层
纯VBA代码_拆分PDF文件_QQ14885553.zip
(931.13 KB, 下载次数: 39)
- Rem 测试代码
- Sub Test()
- Call ExtractPDFpages(StrPDFfile:=ThisWorkbook.Path & "\A.PDF", StrOutFolder:=ThisWorkbook.Path & "\A", StrFG:="_P")
- Rem 提取d:\Guide.PDF,并保存为:d:\output\Guide-p1.PDF、d:\output\Guide-p2.PDF、d:\output\Guide-p3.PDF……
- End Sub
- Sub ExtractPDFpages(ByVal StrPDFfile As String, Optional ByVal StrOutFolder As String = "", Optional ByVal StrFG As String = "_P")
- Rem 函数:ExtractPDFpages 调用Acrobat提取PDF的单个页面并输出到指定目录(命名规则为[原文件名 - p#.PDF])
- Rem 参数: StrPDFfile 原始PDF文件的完整路径
- Rem 参数: StrOutFolder 提出的PDF页面的保存目录, 空白则=本身所在文件夹
- Rem 参数: StrFG 重命名的前缀 空白则=[_P]
- Rem 方法: Call ExtractPDFpages(StrPDFfile:=ThisWorkbook.Path & "\A.PDF", StrOutFolder:=ThisWorkbook.Path & "\A", StrFG:="_P")
- Rem 注意:需要在装有【Acrobat Professional】专业版软件的电脑上运行
- Dim PDF, PDFSource, FSO
- Dim iPageCount As Integer
- Dim sFileName As String
- Rem 原始PDF文件名,去掉文件夹名
- sFileName = Mid(StrPDFfile, InStrRev(StrPDFfile, "") + 1)
- sFileName = Left(sFileName, InStrRev(sFileName, ".") - 1)
- Rem 用Acrobat打开
- Set PDF = CreateObject("AcroExch.PDDoc")
- Set PDFSource = CreateObject("AcroExch.PDDoc")
- PDFSource.Open StrPDFfile
- iPageCount = PDFSource.GetNumPages '//获得总页数
- Rem 输出用的文件夹名
- If StrOutFolder = "" Then
- StrOutFolder = Mid(StrPDFfile, 1, InStrRev(StrPDFfile, ""))
- End If
- If Right(StrOutFolder, 1) <> "" Then
- StrOutFolder = StrOutFolder & ""
- End If
- Rem 创建文件夹
- Set FSO = CreateObject("Scripting.FileSystemObject")
- If FSO.FolderExists(StrOutFolder) = False Then
- MkDir StrOutFolder '//创建文件夹
- End If
- Rem 每一页单独保存
- For I = 0 To iPageCount - 1
- PDF.Create '//新建一个页面
- PDF.InsertPages -1, PDFSource, I, 1, 0 '//将指定页面放入空白页面
- Rem 另存为
- PDF.Save 1, StrOutFolder & sFileName & StrFG & Format(I + 1, "0000") & ".PDF"
- PDF.Close
- Next
- Rem 关闭Acrobat
- PDFSource.Close
- Set PDF = Nothing
- Set PDFSource = Nothing
-
- End Sub
复制代码 |
|