【问题标题】:VBA Adobe Acrobat Sub failing after being successful previouslyVBA Adob​​e Acrobat Sub 之前成功后失败
【发布时间】:2021-07-21 05:23:45
【问题描述】:

我有一个子例程负责将 22 个 pdf 合并为 1。它抓取列表中的第一个 PDF,然后循环通过 i+1 一直到 n(其中 n = 22),将这些页面插入到第一个PDF,然后删除位置 i 处的 pdf。所以最终产品是 1 个 PDF,其中包含所有 22 个 pdf,并且 22 个 pdf 被删除以不使文件路径膨胀。疯狂的是,当这个脚本一直在工作时,它不再工作了!脚本跳过并退出 for 循环,不合并任何内容。

我已经通过并注意到 MergedDoc.GetNumPages() 调用(可在 Adob​​e 的 Interapplication API 文档中找到)返回 -1,因此根据docs.. 与 If "MergedDoc.InsertPages..." 条件语句一样,它退出 for..

但以前这些事情并没有失败!也许文档没有在 .Open() 调用中成功打开,但为什么会这样呢?

有人知道问题可能是什么吗? 我也在 VBA 的工具 -> 参考窗口中包含了 Adob​​e Acrobat 10.0 类型库。我目前也在我的机器上使用 Adob​​e Acrobat DC。代码如下,欢迎任何输入。

谢谢!

Sub MergePDFs(FileList As Variant)
    Dim i As Integer
    'Remember to include Acrobat (tools -> References)
    Dim AcroApp As Acrobat.CAcroApp
    Dim finalPath As String

    Dim numPages As Integer

    Set AcroApp = CreateObject("AcroExch.App")

    Set MergedDoc = CreateObject("AcroExch.PDDoc")
    Set DocToAdd = CreateObject("AcroExch.PDDoc")

    finalPath = FileList(0)
    
    'open first file in PDF Array
    'MergedDoc.Open ("C:\Users\akhawaja\Documents\_a.pdf")
    MergedDoc.Open (finalPath)
    
    MsgBox "Files being combined to path: " & finalPath
    For i = LBound(FileList) + 1 To UBound(FileList)
        'Loop through 2nd - last.
        '1) Open & Get # of pages
        '2)Insert pages, Save, exit
        'MsgBox FileList(i)
        DocToAdd.Open (FileList(i))
        
        
        ' Insert the pages of Part2 after the end of Part1
        numPages = MergedDoc.GetNumPages()
        
        'MsgBox numPages
        'MsgBox DocToAdd.GetNumPages()
        
        If MergedDoc.InsertPages(numPages - 1, DocToAdd, 0, DocToAdd.GetNumPages(), 0) = False Then Exit For
            'MsgBox "Cannot insert pages at doc: " & FileList(i)
        'End If
        
        If MergedDoc.Save(PDSaveFull, finalPath) = False Then Exit For
        'MsgBox "Cannot save the modified document"
        'End If
    
        DocToAdd.Close
        
        'Delete PDF file now that is has been added
        Kill (FileList(i))
    Next i
    
    MergedDoc.Close
    AcroApp.Exit
    Set AcroApp = Nothing
    Set MergedDoc = Nothing
    Set DocToAdd = Nothing

    MsgBox "Done"

End Sub

【问题讨论】:

  • 尝试在numPages = MergedDoc.GetNumPages() 上休息一下,然后再继续:它会报告正确的页数吗?
  • @TimWilliams 不,破坏后它仍然是-1
  • 您的 PDF 的来源是否已更改?
  • @TimWilliams - 不,PDF 文件位置被传递到子例程中并被创建,当我将 FileList 添加到手表时,它会填充有效的文件目录 URL
  • 我的意思是 PDF 来自哪里? (即它们是如何产生的,而不是它们所在的文件夹)

标签: vba api pdf adobe


【解决方案1】:

刚刚弄清楚 - 该路径被用作 OneDrive URL,当我将文件夹更改为带有 C:\ url 的路径时,它最终没有问题。我知道很奇怪。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 2012-09-06
    • 2017-12-26
    • 2019-01-02
    • 2020-05-26
    • 2022-12-29
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    相关资源
    最近更新 更多