vba - VBA Adobe Acrobat Sub 之前成功后失败
问题描述
我有一个负责将 22 个 pdf 合并为 1 的子例程。它抓取列表中的第一个 PDF,然后通过 i+1 一直循环到 n(其中 n = 22),将这些页面插入到第一个 PDF,然后删除位置 i 处的 pdf。所以最终产品是 1 个 PDF,其中包含所有 22 个 pdf,并且 22 个 pdf 被删除以不使文件路径膨胀。疯狂的是,当这个脚本一直在工作时,它不再工作了!该脚本跳过并退出 for 循环,而不合并任何内容。
我已经逐步完成并注意到MergedDoc.GetNumPages()调用(在 Adobe 的 Interapplication API Docs 中找到)返回-1,因此根据文档它失败了。如果“MergedDoc. InsertPages..."条件语句,退出 for..
但以前这些事情并没有失败!也许文档没有在 .Open() 调用中成功打开,但为什么会这样呢?
有人知道问题可能是什么吗?我还从工具 -> 参考窗口中将Adobe Acrobat 10.0 类型库包含在 VBA 中。我目前也在我的机器上使用 Adobe 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
解决方案
刚刚想通了 - 该路径被用作 OneDrive URL,当我将文件夹更改为带有 C:\ url 的路径时,它最终没有问题。我知道很奇怪。谢谢您的帮助!
推荐阅读
- r - 如何根据不同数据框的两个 ID 列的匹配从数据框列中提取值?
- python - 二进制图像上的 Tesseract OCR
- plsql - 如何为游标的选择语句中使用的失败条件引发异常
- symfony - 移动到另一个托管 Symfony 2 后网站关闭
- javascript - 无法在 JavaScript 中获取 40MB 的 JSON 文件
- batch-file - 如何安全地回显 FOR 变量 %%~p 后跟字符串文字
- javascript - 有没有办法将javascript Date 对象转换为字符串,但保留在 Date 对象上使用 console.log 时显示的格式?
- html - 我无法从我的输入中获取我的值以存储在我的 app.component.ts 中
- c# - 如何将常规属性转换为自动属性 C#
- kubernetes - 如何配置 filebeat 守护程序集以处理多个日志记录模板