首页 > 解决方案 > VBA Adob​​e Acrobat Sub 之前成功后失败

问题描述

我有一个负责将 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 Docs 中找到)返回-1,因此根据文档它失败了。如果“MergedDoc. InsertPages..."条件语句,退出 for..

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

有人知道问题可能是什么吗?我还从工具 -> 参考窗口中将Adob​​e Acrobat 10.0 类型库包含在 VBA 中。我目前也在我的机器上使用 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

标签: vbaapipdfadobe

解决方案


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


推荐阅读