首页 > 解决方案 > 将 PDF 拆分为每页的新文件 - Excel VBA

问题描述

我正在尝试将 pdf 每页拆分为多个新文件。我在Excel 论坛上找到了这段代码, 我将其修改为适合我的文件夹路径和文件。我还将 Acrobat.tdl 库添加到引用中。(没有安装实际的 acrobat pro。按照此链接中建议的步骤操作)

但是,当我尝试运行代码时,出现错误 - 运行时错误“429”:ActiveX 组件无法创建对象。错误发生在线路上Set PDDoc = CreateObject("AcroExch.pdDoc")

这是完整的代码:

Sub SplitPDF()
  Dim PDDoc As Acrobat.CAcroPDDoc, newPDF As Acrobat.CAcroPDDoc
  Dim PDPage As Acrobat.CAcroPDPage
  Dim thePDF As String, PNum As Long
  Dim f As String, i As Integer, Result As Variant, NewName As String
  
  f = ThisWorkbook.Path & "\"
  thePDF = f & "CDE_9740240D_2020-09-08.pdf"
  
  Set PDDoc = CreateObject("AcroExch.pdDoc")
  Result = PDDoc.Open(thePDF)
  If Not Result Then
     MsgBox "Can't open file: " & thePDF
     Exit Sub
  End If
  
  '...
  PNum = PDDoc.GetNumPages
  
  For i = 0 To PNum - 1
    Set newPDF = CreateObject("AcroExch.pdDoc")
    newPDF.Create
    NewName = f & " Page_" & i & "_of_" & PNum & ".pdf"
    newPDF.InsertPages -1, PDDoc, i, 1, 0
    newPDF.Save 1, NewName
    newPDF.Close
    Set newPDF = Nothing
  Next i
End Sub

有人可以帮我使这个代码工作。

标签: excelvbapdf

解决方案


推荐阅读